地理编码android-经度和纬度值不正确
我有一个地址,我想知道它的坐标。例如,地址是纽约皇后区的“斯基尔曼大街”。根据maps.google.com,坐标为:地理编码android-经度和纬度值不正确,android,Android,我有一个地址,我想知道它的坐标。例如,地址是纽约皇后区的“斯基尔曼大街”。根据maps.google.com,坐标为:40.747281,-73.9283169。在我的应用程序中,我有如下功能: public GeoPoint addressToGeo(String adr) { Geocoder coder = new Geocoder(this); List<Address> address = null; GeoPoint coordinates;
40.747281,-73.9283169
。在我的应用程序中,我有如下功能:
public GeoPoint addressToGeo(String adr) {
Geocoder coder = new Geocoder(this);
List<Address> address = null;
GeoPoint coordinates;
try {
address = coder.getFromLocationName(adr, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
coordinates = new GeoPoint((int) (location.getLatitude() *1E6),
(int) (location.getLongitude() * 1E6));
return coordinates;
}
如果你看一下纬度和经度变量,这似乎是正确的。但当我输入此代码时:
GeoPoint test;
test = addressToGeo("Skillman Ave");
double latitude = test.getLatitudeE6();
double longitude = test.getLongitudeE6();
String lat = Double.toString(latitude);
String lng = Double.toString(longitude);
String total = lat + " " + lng;
toAdress.setText(total);
ToAddress文本字段将包含4.0747281E7,-7.3928316E7
逗号不在正确的位置,每个双精度结尾的E7
是什么 试试这个
String lat = Double.toString(latitude);
String lng = Double.toString(longitude);
lat= (float) (lat / 1E6);
lng = (float)(lon / 1E6);
System.out.println("lat :" + (float) lat / 1E6);
System.out.println("lon :" + (float) lon / 1E6);
“E7”是一种表示法,表示需要乘以10^7才能得到实际数字。在这种情况下,它将为您提供40747281。然后需要将其格式化为适当的坐标
Ankit的代码似乎可以做到这一点,但需要进行测试以确保。您已经获得了所有正确的数据,所以这个问题实际上是关于格式化一个double。使用 使用此选项显示测试中的纬度/经度点:
DecimalFormat formatter = new DecimalFormat("0.0000000");
String lat = formatter.format(test.getLatitudeE6() / 1E6);
String lon = formatter.format(test.getLongitudeE6() / 1E6);
toAddress.setText(lat + " " + lon);
DecimalFormat formatter = new DecimalFormat("0.0000000");
String lat = formatter.format(test.getLatitudeE6() / 1E6);
String lon = formatter.format(test.getLongitudeE6() / 1E6);
toAddress.setText(lat + " " + lon);