[英]Get zip code from longitude, latitude - android - Java
我已经在SO上尝试了几种可能的“解决方案”,但是没有任何效果适合我。 基本上,我正在尝试从lat / lng获取邮政编码。 我读到Geocoder
和仿真器Genymotion有一些问题,但是我不确定是否确实是问题所在。 此外,我还尝试通过HTTP Request作为JSON对象获取邮政编码,但是这也不起作用。
这是我的代码:
@Override
public void onLocationChanged(Location location) {
EditText zipCode = (EditText) findViewById(R.id.zip_code);
zipCode.setText(getZipCodeFromLocation(location));
}
private String getZipCodeFromLocation(Location location) {
Address addr = getAddressFromLocation(location);
return addr.getPostalCode() == null ? "" : addr.getPostalCode();
}
private Address getAddressFromLocation(Location location) {
Geocoder geocoder = new Geocoder(this);
Address address = new Address(Locale.getDefault());
try {
List<Address> addr = geocoder.getFromLocation(
location.getLatitude(), location.getLongitude(), 1);
if (addr.size() > 0) {
address = addr.get(0);
}
} catch (IOException e) {
e.printStackTrace();
}
return address;
}
问题是, addr.size()
为0,我不知道为什么。
任何帮助将不胜感激。
我以前遇到过这样的问题,但是由于您没有发布日志,所以不知道与您的问题是否相同。 如果您的logcat中出现“服务不可用”错误,则Geocoder可能存在错误。 https://stackoverflow.com/a/4045287/687245 。 如果是这样,有人提供了解决方法,可通过对Geocode的GET调用来查询它。
这是我从Geocode获取纬度和经度的一种解决方法。 有人将此代码发布在Stackoverflow上,所有功劳都归功于他。 您要做的就是将其更改为通过纬度和经度查询,而不是字符串查询,并检索邮政编码而不是纬度。
public void updateLocationByQuery(final String query) {
if (mRunning)
return;
new AsyncTask<Void, Void, LatLng>() {
protected void onPreExecute() {
mRunning = true;
};
@Override
protected LatLng doInBackground(Void... params) {
LatLng p = null;
Geocoder geoCoder = new Geocoder(getActivity(),
Locale.getDefault());
try {
List<Address> addresses = geoCoder.getFromLocationName(
query, 1);
if (addresses.size() > 0) {
p = new LatLng(addresses.get(0).getLatitude(),
addresses.get(0).getLongitude());
} else {
Toast.makeText(getActivity(),
"Unable to find location by query: " + query,
Toast.LENGTH_LONG).show();
}
} catch (Exception ignored) {
// after a while, Geocoder start to trhow
// "Service not availalbe" exception. really weird since it
// was working before (same device, same Android version
// etc..
ignored.printStackTrace();
}
if (p != null) // i.e., Geocoder succeed
{
return p;
} else // i.e., Geocoder failed
{
return fetchLocUsingGoogleMap(query);
}
}
// Geocoder failed :-(
// Our B Plan : Google Map
private LatLng fetchLocUsingGoogleMap(String query) {
double lng = 0, lat = 0;
query = query.replace("\n", " ").replace(" ", "%20");
String googleMapUrl = "http://maps.google.com/maps/api/geocode/json?address="
+ query + "&ka&sensor=false";
try {
JSONObject googleMapResponse = new JSONObject(
ANDROID_HTTP_CLIENT.execute(new HttpGet(
googleMapUrl), new BasicResponseHandler()));
lng = ((JSONArray) googleMapResponse.get("results"))
.getJSONObject(0).getJSONObject("geometry")
.getJSONObject("location").getDouble("lng");
lat = ((JSONArray) googleMapResponse.get("results"))
.getJSONObject(0).getJSONObject("geometry")
.getJSONObject("location").getDouble("lat");
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return new LatLng(lat, lng);
}
@Override
protected void onPostExecute(LatLng loc) {
mRunning = false;
if (loc != null) {
moveCamera(loc);
}
};
}.execute();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.