我已经实现了这段代码来显示基于GPS的地址。纬度和经度工作正常(出现在屏幕上)但是,对于地址,它代表“没有找到地址”。请查看此代码并指出任何错误。谢谢
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_find__location);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager =(LocationManager)getSystemService(context);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(criteria, true);
//String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
//buttons assigned
Button mainMenuBtn = (Button) findViewById(id.mainMenuBtn);
mainMenuBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private final LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
};
private void updateWithNewLocation(Location location) {
String latLong;
TextView myLocationText;
myLocationText = (TextView)findViewById(R.id.myLocationText);
String addressString = "no address found";
if(location != null){
double lat = location.getLatitude();
double lng = location.getLongitude();
latLong = "Lat: "+lat+"\nLong: "+lng;
//double latitude = location.getLatitude();
//double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this,Locale.getDefault());
try{
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
StringBuilder sb = new StringBuilder();
if (addresses.size() > 0){
Address address = addresses.get(0);
for(int i=0; i < address.getMaxAddressLineIndex(); i++)
sb.append(address.getAddressLine(i)).append("\n");
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}catch (IOException e){}
}else{
latLong = "No location found";
}
myLocationText.setText("Your coordinates are:\n"+latLong + "\n"+addressString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.find__location, menu);
return true;
}
}
答案 0 :(得分:0)
我遇到了同样的问题,非常讨厌。一些答案表明地理编码器并不总是返回一个值,所以你必须循环一些。然而,这不是一个好方法,因为你可能永远不会得到一个值,有时候就是这种情况。因此,我在我的项目中所做的是使用谷歌的反向地理编码API。想法是一样的,但方法论是不同的。
这是api页面:https://developers.google.com/maps/documentation/geocoding/
在此处查看第一个答案:Android Geocoder getFromLocationName always returns null 希望这有帮助
答案 1 :(得分:0)
我更喜欢使用Google地理编码API获取地址,通过发送lat lon params,您将获得一些信息 https://developers.google.com/maps/documentation/geocoding/