2012-02-22 43 views
0

我有一个地址,我想知道坐标。例如,地址是纽约州皇后区的“Skillman Ave”。根据maps.google.com,坐标为:40.747281, -73.9283169。在我的应用程序,我有这样的功能:地理编码android - 经度和纬度值不正确

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; 
} 

这需要一个地址作为参数,并希望它会返回坐标。我说,调试器列表中ADRESS的第一个元素包含以下信息:

[Address[addressLines=[0:"Skillman Ave",1:"Queens, New York",2:"Amerikas forente stater"],feature=Skillman Ave,admin=New York,sub-admin=Queens,locality=Queens,thoroughfare=Skillman Ave,postalCode=null,countryCode=US,countryName=Amerikas forente stater,hasLatitude=true,latitude=40.747281,hasLongitude=true,longitude=-73.9283169,phone=null,url=null,extras=null]] 

如果你看的经度和纬度的变量,这似乎是正确的。但是,当我在此代码键入:

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); 

的toAdress文本框将包含4.0747281E7, -7.3928316E7逗号是不是在正确的位置,什么是每个双末端的E7

回答

2

试试这个。

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); 
2

“E7”的符号意义,你需要10^7乘以获得的实际数量。在这种情况下,它会给你40747281.然后你需要将它格式化成适当的坐标。

Ankit的代码看起来像可能会这样做,但测试以确保。

1

你已经得到了所有正确的数据,所以这个问题真的是关于格式化一个双。使用DecimalFormat

使用此显示经/纬度在您的测试点:

DecimalFormat formatter = new DecimalFormat("0.0000000"); 
String lat = formatter.format(test.getLatitudeE6()/1E6); 
String lon = formatter.format(test.getLongitudeE6()/1E6); 
toAddress.setText(lat + " " + lon); 
相关问题