lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0, this);
Location location=lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
geocoder = new Geocoder(this);
if(location!=null)
onLocationChanged(location);
public void onLocationChanged(Location location) {
try {
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(), 2); // <10>
for (Address address : addresses) {
tv.setText(address.getAddressLine(0));
}
}catch(Exception e){
}
这是我的示例代码。我创建了一个扩展Locationlistener的类,我也实现了其他抽象类。我能够使用此代码获取城市名称。现在它不起作用。
此代码中有错误吗?
答案 0 :(得分:0)
试试这个....可能会有所帮助
String add1 = "";
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
geoLatitude = p.getLatitudeE6()/1E6;
geoLongitude = p.getLongitudeE6()/1E6;
try {
List<Address> addresses = geoCoder.getFromLocation(p.getLatitudeE6()/1E6, p.getLongitudeE6()/1E6, 1);
if(addresses.size() > 0 ) {
for(int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++) {
add1 += addresses.get(0).getAddressLine(i) + "\n";
}
}
}catch(IOException ioe) {
ioe.printStackTrace();
}