2012-06-18 65 views
14

如何在Android中獲取國家/地區名稱,如果已知地理座標?如何使它成爲最簡單的方法?從座標獲取國家嗎?

+0

有幾種方法可以做到這一點:http://stackoverflow.com/questions/6922312/get-location-name-from-fetched-coordinates – gunar

回答

13

使用以下函數,該函數。

public void getAddress(double lat, double lng) { 
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault()); 
    try { 
     List<Address> addresses = geocoder.getFromLocation(lat, lng, 1); 
     Address obj = addresses.get(0); 
     String add = obj.getAddressLine(0); 
     GUIStatics.currentAddress = obj.getSubAdminArea() + "," 
       + obj.getAdminArea(); 
     GUIStatics.latitude = obj.getLatitude(); 
     GUIStatics.longitude = obj.getLongitude(); 
     GUIStatics.currentCity= obj.getSubAdminArea(); 
     GUIStatics.currentState= obj.getAdminArea(); 
     add = add + "\n" + obj.getCountryName(); 
     add = add + "\n" + obj.getCountryCode(); 
     add = add + "\n" + obj.getAdminArea(); 
     add = add + "\n" + obj.getPostalCode(); 
     add = add + "\n" + obj.getSubAdminArea(); 
     add = add + "\n" + obj.getLocality(); 
     add = add + "\n" + obj.getSubThoroughfare(); 

     Log.v("IGA", "Address" + add); 
     // Toast.makeText(this, "Address=>" + add, 
     // Toast.LENGTH_SHORT).show(); 

     // TennisAppActivity.showDialog(add); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show(); 
    } 
} 

添加以下權限到你的清單

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
+0

你的代碼有一個錯誤。有時候,從'lat'和'lng',你沒有地址。這意味着'address.get(0)'引發'IndexOutOfBound'異常。 – allenlsy

24

在這裏你去

Geocoder gcd = new Geocoder(context, Locale.getDefault()); 
List<Address> addresses = gcd.getFromLocation(lat, lng, 1); 

    if (addresses.size() > 0) 
    { 
     String countryName=addresses.get(0).getCountryName(); 
    } 
3

使用本

try { 
     Geocoder geo = new Geocoder(this.getApplicationContext(), Locale.getDefault()); 
     List<Address> addresses = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 1); 
     if (addresses.isEmpty()) { 
      placeName.setText("Waiting for Location"); 
     } 
     else { 
      if (addresses.size() > 0) { 
       placeName.setText(addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName()); 
      } 
     } 
    } 
    catch(Exception e){ 
     Toast.makeText(this, "No Location Name Found", 600).show(); 
    } 

使用該清單文件

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
2

我怎麼做的近期正在讀來自Web API URL和解析的數據JSON。

一個例子網址爲點(40.714224,-73.961452)是:

http://maps.google.com/maps/geo?q=40.714224,-73.961452&output=json&oe=utf8&sensor=true_or_false&key=your_api_key 

將會產生以下的輸出:

{ 
    "name": "40.714224,-73.961452", 
    "Status": { 
    "code": 200, 
    "request": "geocode" 
    }, 
    "Placemark": [ { 
    "id": "p1", 
    "address": "285 Bedford Ave, Brooklyn, NY 11211, USA", 
    "AddressDetails": { 
    "Accuracy" : 8, 
    "Country" : { 
     "AdministrativeArea" : { 
     "AdministrativeAreaName" : "NY", 
     "SubAdministrativeArea" : { 
      "Locality" : { 
       "DependentLocality" : { 
        "DependentLocalityName" : "Williamsburg", 
        "PostalCode" : { 
        "PostalCodeNumber" : "11211" 
        }, 
        "Thoroughfare" : { 
        "ThoroughfareName" : "285 Bedford Ave" 
        } 
       }, 
       "LocalityName" : "Brooklyn" 
      }, 
      "SubAdministrativeAreaName" : "Kings" 
     } 
     }, 
     "CountryName" : "USA", 
     "CountryNameCode" : "US" 
    } 
}, 
    "ExtendedData": { 
     "LatLonBox": { 
     "north": 40.7154779, 
     "south": 40.7127799, 
     "east": -73.9600584, 
     "west": -73.9627564 
     } 
    }, 
    "Point": { 
     "coordinates": [ -73.9614074, 40.7141289, 0 ] 
    } 
    } ] 
} 

我發現GSON成爲Android中解析JSON相當不錯。