2012-08-13 128 views

回答

4
private void getFromLocation(String address) 
    { 
      double latitude= 0.0, longtitude= 0.0; 

     Geocoder geoCoder = new Geocoder(this, Locale.getDefault());  
     try 
     { 
      List<Address> addresses = geoCoder.getFromLocationName(address , 1); 
      if (addresses.size() > 0) 
      {    
       GeoPoint p = new GeoPoint(
         (int) (addresses.get(0).getLatitude() * 1E6), 
         (int) (addresses.get(0).getLongitude() * 1E6)); 

       latitude=p.getLatitudeE6()/1E6; 
       longtitude=p.getLongitudeE6()/1E6; 


       } 
     } 
     catch(Exception ee) 
     { 

     } 
    } 
} 
+0

沒有的GeoPoint類在這裏? – 2017-06-21 06:57:27

+0

這是舊的答案。您需要在build.gradle – Kamal 2017-06-22 04:17:23

+0

中添加google map apis,或者您可以直接從Address對象獲取經度和緯度。 – Kamal 2017-06-22 04:19:22

1

這會給你(循環)地址串的所有匹配。 如果你只是想要的第一場比賽,確保address.size()是偉大大於0,則取address.get(0);

在我的使用,我得到的所有比賽,並告訴他們作爲選項。用戶然後點擊一個,並選擇該GPS位置。

Geocoder geocoder = new Geocoder(getBaseContext()); 
List<Address> addresses; 
try { 
    addresses = geocoder.getFromLocationName("Example StreeT, UK, DNFE", 20); 

    for(int i = 0; i < addresses.size(); i++) { // MULTIPLE MATCHES 

    Address addr = addresses.get(i); 

    double latitude = addr.getLatitude(); 
    double longitude = addr.getLongitude(); // DO SOMETHING WITH VALUES 

    } 

} 
+0

謝謝你的快速回復..我會試試這個 – diordna 2012-08-13 10:32:00

1

您可以從下面的代碼獲得,

private void GetLatitudeAndLongitude() { 
    geocoder = new Geocoder(mContext, Locale.getDefault()); 
    try { 
     List<Address> addresses = geocoder.getFromLocationName(txtLocation.getText().toString().trim().toLowerCase(), 1); 
     if (addresses.size() > 0) { 
      homeInfoModel.setLalitude(String.valueOf(addresses.get(0).getLatitude())); 
      homeInfoModel.setLongitude(String.valueOf(addresses.get(0).getLongitude())); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
}