我希望在Android中实现反向地理编码,但我遇到一些问题,有人可以给我一些指示吗? 这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textLong = (TextView) findViewById(R.id.textLong);
textLat = (TextView) findViewById(R.id.textLat);
LocationManager manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener listener = new myLocationListner();
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, listener);
}
public class myLocationListner implements LocationListener{
@Override
public void onLocationChanged(Location location) {
if (location != null){
double gettextLong = location.getLongitude();
double gettextLat = location.getLatitude();
textLat.setText(Double.toString(gettextLat));
textLong.setText(Double.toString(gettextLong));
}
答案 0 :(得分:0)
您需要创建Geocoder对象并使用.getFromLocation方法:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(gettextLat, getTextLong, 1);
即:
public class myLocationListner implements LocationListener{
Geocoder mGeocoder;
// default constructor
myLocationListener() {
// instantiate geocoder object:
mGeocoder = new Geocoder(this, Locale.getDefault());
}
@Override
public void onLocationChanged(Location location) {
if (location != null){
double gettextLong = location.getLongitude();
double gettextLat = location.getLatitude();
textLat.setText(Double.toString(gettextLat));
textLong.setText(Double.toString(gettextLong));
List<Address> addresses = mGeocoder.getFromLocation(gettextLat, getTextLong, 1);
}
}
请参阅:http://developer.android.com/reference/android/location/Geocoder.html