我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
当前回答
使用Geocoder类从纬度和经度中获得完整地址非常容易。下面是代码示例。希望这能有所帮助!
if (l != null) {
val lat = l.latitude
val lon = l.longitude
val geocoder = Geocoder(this, Locale.getDefault())
val addresses: List<Address>
addresses = geocoder.getFromLocation(lat, lon, 1)
val address = addresses[0].getAddressLine(0)
val address2 = addresses[0].getAddressLine(1)
val city = addresses[0].locality
val state = addresses[0].adminArea
val country = addresses[0].countryName
val postalCode = addresses[0].postalCode
val knownName = addresses[0].featureName
val message =
"Emergency situation. Call for help. My location is: " + address + "." + "http://maps.google.com/maps?saddr=" + lat + "," + lon
}
您可以只使用地址值,因为它提供了所有完整的地址。如果需要单独的组件,也可以使用其他组件。
其他回答
在onCreate () . .
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, this);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location == null) {
Toast.makeText(getApplicationContext(), "GPS signal not found",
3000).show();
}
if (location != null) {
Log.e("location", "location--" + location);
Log.e("latitude at beginning",
"@@@@@@@@@@@@@@@" + location.getLatitude());
onLocationChanged(location);
}
在onLocationChanged()中编写代码
@Override
public void onLocationChanged(Location location) {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.e("latitude", "latitude--" + latitude);
try {
Log.e("latitude", "inside latitude--" + latitude);
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName();
locationTxt.setText(address + " " + city + " " + country);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
1 -在onCreate方法中为LocationManager和LocationListener创建变量。
2 -检查是否有权限,因此执行位置更新并从locationManager获取lastKnownLocation,否则您将请求权限
3 -在主类中创建onRequestPermissionResult并检查是否有权限,然后执行位置更新
4 -创建分离的方法,其中包括Geocoder变量,并创建一个列表,从您的位置放置坐标, 所以为了安全起见,你检查列表是否存在,如果我们想在该列表中的每个信息是存在的,那么你使用(get通道==>街道地址),(getLocality ==>城市/州),(getPostalCode ==> Zip), (getAdminArea ==>完整地址)
5 -最后,在检查权限后调用该方法(lastKnownLocation参数==>,当应用程序运行时显示地址)和onLocationChanged with(位置参数==>,当位置改变时显示地址)
代码部分:
LocationManager locationManager;
LocationListener locationListener;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
updateLocation(location);
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateLocation(lastKnownLocation);
}else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
public void updateLocation ( Location location){
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> listAddresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
String address = "Could not find location :(";
if (listAddresses != null && listAddresses.size() > 0) {
if (listAddresses.get(0).getThoroughfare() != null) {
address = listAddresses.get(0).getThoroughfare() + " ";
}
if (listAddresses.get(0).getLocality() != null) {
address += listAddresses.get(0).getLocality() + " ";
}
if (listAddresses.get(0).getPostalCode() != null) {
address += listAddresses.get(0).getPostalCode() + " ";
}
if (listAddresses.get(0).getAdminArea() != null) {
address += listAddresses.get(0).getAdminArea();
}
}
Log.i("Address",address);
} catch (Exception e) {
e.printStackTrace();
}
}
}
接受的答案在kotlin格式
private fun getAddressInfo(latitude:Double, longitude:Double){
val geocoder = Geocoder(this, Locale.getDefault())
val addresses: List<Address> = geocoder.getFromLocation(latitude, longitude, 1)
val address: String = addresses[0].getAddressLine(0)
val city: String = addresses[0].locality
val state: String = addresses[0].adminArea
val country: String = addresses[0].countryName
val postalCode: String = addresses[0].postalCode
val knownName: String = addresses[0].featureName
}
使用地理编码器,你可以得到这样的东西!
try {
Geocoder geo = new Geocoder(MapsActivity.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(origin.latitude, origin.longitude, 1);
address.setText("Loading...");
if (addresses != null && addresses.size() > 0) {
String locality = addresses.get(0).getAddressLine(0);
String country = addresses.get(0).getCountryName();
String state = addresses.get(0).getAdminArea();
String sub_admin = addresses.get(0).getSubAdminArea();
String city = addresses.get(0).getFeatureName();
String pincode = addresses.get(0).getPostalCode();
String locality_city = addresses.get(0).getLocality();
String sub_localoty = addresses.get(0).getSubLocality();
if (locality != null && country != null) {
address.setText(locality + ", " + (sub_localoty != null ? sub_localoty + ", " : "") + (locality_city != null ? locality_city + ", " : "" ) + (city != null ? city + ", " : "") + (sub_admin != null ? sub_admin + ", " : "") + (state != null ? state + ", " : "") + country + ", " + (pincode != null ? pincode : ""));
} else {
address.setText("Location could not be fetched...");
}
}
} catch (Exception e) {
address.setText("Location could not be fetched...");
e.printStackTrace(); // getFromLocation() may sometimes fail
}
从latlong (Geo-coordinates)获取Address还有最后一个技巧。你可以简单地通过经度和纬度点击谷歌地图web服务。它只是一个GET-Method web服务。
它将返回JSON响应,可以很容易地解析以获得地址。它的URL是:
http://maps.googleapis.com/maps/api/geocode/json?latlng=32,75&sensor=true
你可以用lat,long代替32,75。