我想找出Android应用程序中的经度和纬度。我正在使用的代码如下: -从经度和纬度没有得到邮政编码
protected int findZipCode() {
LocationManager lm = (LocationManager) getApplicationContext()
.getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 60000,
1000, listener);
if (zipCode == null) {
return 0;
} else {
return Integer.parseInt(zipCode);
}
}
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
Geocoder geo = new Geocoder(getApplicationContext());
List<Address> addr = null;
try {
addr = geo.getFromLocation(location.getLatitude(),
location.getLatitude(), 5);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
zipCode = addr.get(0).getPostalCode();
}
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
但是,此代码似乎始终为邮政编码返回空值。任何帮助都会很棒。
在此先感谢!
你可以PLZ建议我需要在我的代码中做出的调整? –
findZipCodeFunction写在哪里? Listner定义在哪里?在同一活动? – Farhan
是啊,他们都在同一个活动......我需要的人得到的邮政编码,然后我在我的应用程序中的邮政编码的基础上执行搜索... –