2014-11-24 36 views
0

如果我检索的是这样的(34872.1297,41551.7292)字符串变量,所以这将是“(34872.1297,41551.7292)”,我怎么转换这个字符串变量指向(地理位置)?Android的转换坐标的字符串变量指向

例如,这台点值,但我想检索

Point point = new Point(34872.1297,41551.7292); 
+0

什么样点的?地理位置?屏幕点?更具体 – SeahawksRdaBest 2014-11-24 03:16:55

+0

尝试通过parseFloat方法将其转换为浮点值 – 2014-11-24 04:19:27

回答

1

你所寻找的是how to split a string值,并有上这样对你细读了几个很好的例子。

在你的情况,这将工作:

String yourString = "(34872.1297,41551.7292)"; 

// Strip out parentheses and split the string on "," 
String[] items = yourString.replaceAll("[()]", "").split("\\s*,\\s*")); 

// Now you have a String[] (items) with the values "34872.1297" and "41551.7292" 
// Get the x and y values as Floats 
Float x = Float.parseFloat(items[0]); 
Float y = Float.parseFloat(items[1]); 

// Do with them what you like (I think you mean LatLng instead of Point) 
LatLng latLng = new LatLng(x, y); 

添加检查空值和解析异常等

1

与存储的地理坐标的问题作为一个Point对象是点实际需要两个值是整型。所以你会失去信息。

所以,你可以提取和类型转换的坐标是整数(但丢失的信息):

String geo = "(34872.1297,41551.7292)"; 

// REMOVE BRACKETS, AND WHITE SPACES 
geo = geo.replace(")", ""); 
geo = geo.replace("(", ""); 
geo = geo.replace(" ", ""); 

// SEPARATE THE LONGITUDE AND LATITUDE 
String[] split = geo.split(","); 

// ASSIGN LONGITUDE AND LATITUDE TO POINT AS INTEGERS 
Point point = new Point((int) split[0], (int) split[1]); 

或者,你可以提取它们的花车,并将它们存储在其他一些数据类型的离你的选择。

String geo = "(34872.1297,41551.7292)"; 

// REMOVE BRACKETS, AND WHITE SPACES 
geo = geo.replace(")", "");  
geo = geo.replace("(", ""); 
geo = geo.replace(" ", ""); 

// SEPARATE THE LONGITUDE AND LATITUDE 
String[] split = geo.split(","); 

// ASSIGN LONGITUDE AND LATITUDE TO POINT AS INTEGERS 
Float long = (float) split[0]; 
Float lat = (float) split[1]; 

编辑:改变geo.split( “:”),以geo.split( “”)中的代码()(感谢Jitesh)

+1

其中是:(签名:)在地理字符串的基础上应该被分离吗?正如你写的String [] split = geo.split(“:”); – 2014-11-24 04:54:49