Location值是从我们的服务器获取的,现在是字符串格式,我在定义新位置后想获取经度和纬度,但是它是0.0。 代码就是这样。
String location = "lat\/lng: (28.6988812,77.1153696)";
Location loc = new Location(location);
double lat = loc.getLatitude();
double longitude = loc.getLongitude();
答案 0 :(得分:0)
根据Android文档,您需要在构造函数中传递位置提供程序。 请参阅https://developer.android.com/reference/android/location/Location.html#Location(java.lang.String)
您需要解析从服务器获取的字符串(“ lat / lng:(28.6988812,77.1153696)”),提取纬度和经度,然后使用setLatitude()和setLongitude()将这些值传递给位置对象功能。
答案 1 :(得分:0)
您可以使用regex从服务器获取的位置字符串中提取纬度和经度。像这样:
Double latitude = 0., longitude = 0.;
//your location
String location = "lat/long: (28.6988812,77.1153696)";
//pattern
Pattern pattern = Pattern.compile("lat/long: \\(([0-9.]+),([0-9.]+)\\)$");
Matcher matcher = pattern.matcher(location);
if (matcher.matches()) {
latitude = Double.valueOf(matcher.group(1));
longitude = Double.valueOf(matcher.group(2));
}
如果您需要Location对象:
Location targetLocation = new Location("");//provider name is unnecessary
targetLocation.setLatitude(latitude);//your coords of course
targetLocation.setLongitude(longitude);
感谢此answer