2012-02-22 44 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); 
相關問題