2011-02-02 195 views
4

在我目前的Android應用程序中,我想根據輸入的城市名稱,街道名稱或郵政編碼獲取地理座標。我怎樣才能做到這一點?從城市名稱獲取經緯度

最好的問候

回答

0

使用Geocoder類。這並不難,然後你可以使用getFromLocationName,它可以做你想做的。您也可以反過來使用這個類,也就是說,輸入(lat,lon)並獲取位於那裏的地址。

我假設你想通過Android代碼來實現,如果你想使用Javascript,那麼你應該使用Google map's API,正如另一個答案中指出的那樣。

3

searchesAddress - >可以是(城市名稱/地址/郵編)。

public boolean getLatitudeAndLongitudeFromGoogleMapForAddress(String searchedAddress){ 

    Geocoder coder = new Geocoder(IPlant.iPlantActivity); 
    List<Address> address; 
    try 
    { 
     address = coder.getFromLocationName(searchedAddress,5); 
     if (address == null) { 
      Log.d(TAG, "############Address not correct #########"); 
     } 
     Address location = address.get(0); 

     Log.d(TAG, "Address Latitude : "+ location.getLatitude();+ "Address Longitude : "+ location.getLongitude()); 
     return true; 

    } 
    catch(Exception e) 
    { 
     Log.d(TAG, "MY_ERROR : ############Address Not Found"); 
     return false; 
    } 
} 
4

通過網

private static JSONObject getLocationInfo(String address) 
{ 

    StringBuilder stringBuilder = new StringBuilder(); 
    try { 

    address = address.replaceAll(" ","%20");  

    HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); 
    HttpClient client = new DefaultHttpClient(); 
    HttpResponse response; 
    stringBuilder = new StringBuilder(); 


     response = client.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     InputStream stream = entity.getContent(); 
     int b; 
     while ((b = stream.read()) != -1) { 
      stringBuilder.append((char) b); 
     } 
    } catch (ClientProtocolException e) { 
     Log.i("getLocationInfo ClientProtocolException", e.toString()); 
    } catch (IOException e) { 

     Log.i("getLocationInfo IOException", e.toString()); 
    } 


    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject = new JSONObject(stringBuilder.toString()); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     Log.i("getLocationInfo JSONException", e.toString()); 
    } 

    return jsonObject; 
} 

private static boolean getLatLong(JSONObject jsonObject) 
{ 

    try { 

     longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng"); 
     Log.i("Log1", longitute1+""); 
    latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
      .getJSONObject("geometry").getJSONObject("location") 
      .getDouble("lat"); 
     Log.i("lat1", latitude1+""); 
    } catch (JSONException e) { 

     longitute=0; 
     latitude = 0; 
     Log.i("getLatLong", e.toString()); 
     return false; 

    } 

    return true; 
} 
獲得地理座標
相關問題