2013-05-18 53 views
-1

問候,當前位置與谷歌地圖API V2

我只想問有沒有對谷歌地圖API密鑰版本2,我可以獲取當前位置(在地址)通過經緯度任何方法或類的。在Google Map Api版本1中,有一個類Geocoder,我們可以使用它來將經度和緯度更改爲Address.But,但我認爲我們不能在Api v2中使用Geocoder類,因爲它使用Fragment類。任何人都有解決方案?謝謝。

+0

您可以前往這個帖子: http://stackoverflow.com/questions/5314155/get-current-position-location-android –

+0

我不認爲這將是我的價值Lopez。 –

回答

0

Geocoder不是Maps API v1的一部分,您可以將其與API v2一起使用。

+0

你有什麼例子嗎?或者如果你可以給我看一些代碼。其實我做了,但我得到了經度和緯度,但我無法獲得該特定位置的地址。 –

+0

在頂部的搜索框中輸入「geocoder example」給出:http://stackoverflow.com/questions/11430726/geocoding-example-not-working –

+0

好的,謝謝。我會嘗試這一個。 –

0

是的GeoCoder真的很可怕。在某些設備上,這無法正常工作。你可以做到這一點,這是不依賴...做一個json請求:

如果你需要其他方式,該代碼將幫助你。 ADRESS - >位置

public static JSONObject getLocationInfo(String address) { 

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

    try { 
     response = client.execute(httpGet); 
     HttpEntity entity = response.getEntity(); 
     InputStream stream = entity.getContent(); 
     int b; 
     while ((b = stream.read()) != -1) { 
      stringBuilder.append((char) b); 
     } 
    } catch (ClientProtocolException e) { 
    } catch (IOException e) { 
    } 

    JSONObject jsonObject = new JSONObject(); 
    try { 
     jsonObject = new JSONObject(stringBuilder.toString()); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return jsonObject; 
} 

public static GeoPoint getGeoPoint(JSONObject jsonObject) { 

    Double lon = new Double(0); 
    Double lat = new Double(0); 

    try { 

     lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
      .getJSONObject("geometry").getJSONObject("location") 
      .getDouble("lng"); 

     lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
      .getJSONObject("geometry").getJSONObject("location") 
      .getDouble("lat"); 

    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6)); 

}