方法Geocoder.getFromLocationName()
会在Android 4.1上引发异常Service not available
,即使GooglePlayServicesUtil.isGooglePlayServicesAvailable()
返回SUCCESS
而Geocoder.isPresent()
返回true
。
新的Google Maps v2 API中是否有任何地理编码的官方示例?
答案 0 :(得分:3)
Geocoder
与Google Maps Android API v2无关。
您可能希望直接使用Google Geocoding API而不是Geocoder
,这会为您提供有限的数据量,并可能受设备或Android版本特定问题的影响。
答案 1 :(得分:2)
试试这个......
public JSONObject getLocationFormGoogle(String placesName) {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?address=" +placesName+"&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) {
e.printStackTrace();
}
return jsonObject;
}
public LatLng getLatLng(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 LatLng(lat,lon);
}
LatLng Source =getLatLng(getLocationFormGoogle(placesName));