在android
我可以轻松onLocationChanged
上班并获得移动经纬度。
但是,一旦我得到了这些坐标,我怎样才能得到手机的地址,例如
XYWZ Road, GRDSASDF Sector 823432, Australia etc
在android
我可以轻松onLocationChanged
上班并获得移动经纬度。
但是,一旦我得到了这些坐标,我怎样才能得到手机的地址,例如
XYWZ Road, GRDSASDF Sector 823432, Australia etc
Geocoder是一个用于处理Geocoding和Reverse Geocoding的类。
反向地理编码位置描述中的详细信息量可能会有所不同,例如,一个可能包含最近建筑物的完整街道地址,而另一个可能仅包含城市名称和邮政编码。Geocoder 类需要一个不包含在核心 android 框架中的后端服务。如果平台中没有后端服务,Geocoder 查询方法将返回一个空列表。使用该isPresent()
方法确定是否存在 Geocoder 实现。
该getFromLocation(double latitude, double longitude, int maxResults)
方法返回一个已知的地址数组,用于描述紧接给定纬度和经度周围的区域。返回的地址将针对提供给此类的构造函数的语言环境进行本地化。
返回值可以通过网络查找的方式获得。结果是最佳猜测,不保证有意义或正确。从与主 UI 线程不同的线程调用此方法可能很有用。
这是熟悉地理编码器的教程:
public class AndroidFromLocation extends Activity {
double LATITUDE = 37.42233;
double LONGITUDE = -122.083;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView myLatitude = (TextView)findViewById(R.id.mylatitude);
TextView myLongitude = (TextView)findViewById(R.id.mylongitude);
TextView myAddress = (TextView)findViewById(R.id.myaddress);
myLatitude.setText("Latitude: " + String.valueOf(LATITUDE));
myLongitude.setText("Longitude: " + String.valueOf(LONGITUDE));
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
myAddress.setText(strReturnedAddress.toString());
}
else{
myAddress.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
myAddress.setText("Canont get Address!");
}
}
}
您可以使用 Android 中的GeoCoder类。
通过使用方法
List<Address> getFromLocation(double latitude, double longitude, int maxResults)
使用地理编码器方法。
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
首先,你找出你说你已经完成的经纬度。
然后,您可以使用以下代码获取列表中的完整地址。这是代码:
// this will fetch the data of current address
List<Address> addresses=geoCoder.getFromLocation(location.getLatitude(), location.getLongitude(), 10);
int i=1;
for(Address addObj:addresses)
{
// this will make the loop run for 1 time
if(i==1)
{
//variables to split address line
String add_line1_extract;
//setting street address
streetaddressText.setText(addObj.getAddressLine(0));
//splitting city and state
add_line1_extract=addObj.getAddressLine(1);
String string = add_line1_extract;
String[] parts = string.split(",");
//Setting city
part1 = parts[0];
cityText.setText(part1);
//setting state
String part2 = parts[1];
stateText.setText(part2);
//setting country
countryText.setText(addObj.getAddressLine(2));
i++;
progress.setVisibility(View.GONE);
}
}
试试这个代码:
public class LocationSpeecher extends MapActivity{
MapController mc;
MapView myMapView;
MapController mapController;
GeoPoint point;
MyPositionOverlay positionOverlay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location_speecher);
MapView myMapView=(MapView)findViewById(R.id.myMapView);
mapController=myMapView.getController();
myMapView.displayZoomControls(true);
mapController.setZoom(17);
// myMapView.setSatellite(true);
myMapView.setStreetView(true);
myMapView.setTraffic(true);
LocationManager locationManager;
String context = Context.LOCATION_SERVICE;
locationManager = (LocationManager)getSystemService(context);
Criteria crta = new Criteria();
crta.setAccuracy(Criteria.ACCURACY_FINE);
crta.setAltitudeRequired(false);
crta.setBearingRequired(false);
crta.setCostAllowed(true);
crta.setPowerRequirement(Criteria.POWER_LOW);
String provider = locationManager.getBestProvider(crta, true);
// String provider = LocationManager.GPS_PROVIDER;
Location location = locationManager.getLastKnownLocation(provider);
updateWithNewLocation(location);
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener);
}
private final LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
updateWithNewLocation(location);
}
@Override
public void onProviderDisabled(String provider) {
updateWithNewLocation(null);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
};
private void updateWithNewLocation(Location location) {
String latLong;
TextView myLocation;
myLocation = (TextView) findViewById(R.id.myLocation);
String addressString = "no address found";
if(location!=null) {
Double geoLat=location.getLatitude()*1E6;
Double geoLng=location.getLongitude()*1E6;
GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
mapController.animateTo(point);
double lat = location.getLatitude();
double lon = location.getLongitude();
latLong = "Lat:" + lat + "\nLong:" + lon;
double lattitude = location.getLatitude();
double longitude = location.getLongitude();
Geocoder gc = new Geocoder(this,Locale.getDefault());
try {
List<Address> addresses= gc.getFromLocation(lattitude, longitude, 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 (Exception e) {
}
} else {
latLong = " NO Location Found ";
}
myLocation.setText("Current Position is :\n"+ latLong + "\n"+ addressString );
}