Android 地理编码器 getFromLocationName 因有效地址而失败
Android Geocoder getFromLocationName fails with valid address
我正在尝试使用以下方法获取特定地址的纬度和经度
addressList = geoCoder.getFromLocationName(locationName, 1);
对于大多数地址,这工作得很好,但有一些有效地址,如 "Lentoasemantie 1, Vantaa",其中 returns 空数组。奇怪的是,有效地址在 4 天前还可以使用,但现在大部分地址都可以使用了。
所以,这看起来像是 Google 后端问题,我想知道我应该向 Google 报告这个问题(在哪里/如何?)还是不再使用 Geocoder,因为它本质上是不可靠的?
地理编码器 Android API 不如 Google 地图地理编码 API 高效,所以我建议您使用这个而不是地理编码器。
您可以在下面找到通过 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);
}
我正在尝试使用以下方法获取特定地址的纬度和经度 addressList = geoCoder.getFromLocationName(locationName, 1);
对于大多数地址,这工作得很好,但有一些有效地址,如 "Lentoasemantie 1, Vantaa",其中 returns 空数组。奇怪的是,有效地址在 4 天前还可以使用,但现在大部分地址都可以使用了。
所以,这看起来像是 Google 后端问题,我想知道我应该向 Google 报告这个问题(在哪里/如何?)还是不再使用 Geocoder,因为它本质上是不可靠的?
地理编码器 Android API 不如 Google 地图地理编码 API 高效,所以我建议您使用这个而不是地理编码器。 您可以在下面找到通过 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);
}