众所周知,地理编码器在较旧的设备上不可靠(以及在 Genymotion 上,因此我使用了适用于所有环境的辅助类),并且作为一个类通常存在问题(以及结果的准确性)。
您可以使用谷歌地图非常轻松地进行地理定位,
我写的一个异步任务首先使用 Geocoder,如果失败则回退到 HttpRequest(使用 volley)在https://gist.github.com/selecsosi/6705630
相关部分:
提出请求
"http://maps.google.com/maps/api/geocode/json?address=" + URLEncoder.encode(mAddress, "utf-8") + "&ka&sensor=false"
mAddress 是您要搜索的位置。
该请求将返回一个 JSON 对象,您可以像这样解析
public static LatLng getLatLngFromGoogleJson(JSONObject jsonObject) {
double lon = 0d;
double lat = 0d;
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) {
if(Log.isLoggable(TAG, Log.ERROR))Log.e(TAG, "Error parsing google response", e);
}
return new LatLng(lat, lon);
}