我正在尝试使用以下方法获取特定地址的纬度和经度 addressList = geoCoder.getFromLocationName(locationName, 1);
对于大多数地址,这工作得很好,但有一些有效地址,如“Lentoasemantie 1, Vantaa”,它返回空数组。奇怪的是,有效地址在 4 天前还可以使用,但现在大部分地址仍然可以使用。
所以,这看起来像是 Google 后端问题,我想知道我应该向 Google 报告这个问题(在何处/如何?)还是不再使用 Geocoder,因为它本质上是不可靠的?
最佳答案
Geocoder Android API 不如 Google Maps Geocoding API 高效,因此我建议您使用这个 API 而不是 Geocoder。 您可以在下面找到通过 Volley 队列从地址获取位置的函数(对于 Lentoasemantie 1,Vantaa,它有效):
public void getLocationFromAddress(String address) {
String url = "https://maps.googleapis.com/maps/api/geocode/json?address="
+ Uri.encode(address) + "&sensor=true&key=API_KEY";
RequestQueue queue = Volley.newRequestQueue(this);
JsonObjectRequest stateReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONObject location;
try {
// Get JSON Array called "results" and then get the 0th
// complete object as JSON
location = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
// Get the value of the attribute whose name is
// "formatted_string"
if (location.getDouble("lat") != 0 && location.getDouble("lng") != 0) {
LatLng latLng = new LatLng(location.getDouble("lat"), location.getDouble("lng"));
//Do what you want
}
} catch (JSONException e1) {
e1.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("Error.Response", error.toString());
}
});
// add it to the queue
queue.add(stateReq);
}
关于java - Android Geocoder getFromLocationName 因有效地址而失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39063590/