我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
当前回答
在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();
}
}
其他回答
您正在寻找“地理编码”一词。
你需要做的是:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
要做更多,你应该在这里阅读Geocoder。
用这个对我有用:D
检索纬度和经度的json数据。
https://maps.googleapis.com/maps/api/geocode/json?key=AIzaSyAr29XeWWAeWZcrOgjjfs3iSnqkWtAz4No&latlng=2.1812,102.4266&sensor=true
用你自己的地方改变纬度、经度。
https://maps.googleapis.com/maps/api/geocode/json?key= < \ API_KEY_HERE > &latlng =“纬度,经度”&sensor = true
您可以使用自己的密钥更改<\API_KEY_HERE>。
需要在谷歌控制台为新的api密钥启用api服务。
D选项是正确答案
使用地理编码器,你可以得到这样的东西!
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
}
试试这段代码(正在工作)
public void GetLocation() throws IOException {
LocationManager locationManager = (LocationManager) context.getSystemService(LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)) {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
}, 200);
return;
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30000, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.d(TAG, "onLocationChanged: " + location.getLongitude() + " , " + location.getLatitude());
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
Log.d(TAG, "onStatusChanged: " + s);
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
});
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, true);
Location location = locationManager.getLastKnownLocation(bestProvider);
if (location == null) {
Toast.makeText(context, "GPS signal not found",
Toast.LENGTH_LONG).show();
}
if (location != null) {
Log.e("location", "location--" + location);
Log.e("latitude at beginning",
"@@@@@@@@@@@@@@@" + location.getLatitude());
// onLocationChanged(location);
}
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
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();
Log.d(TAG, "GetLocation: address " + address + " city " + city + " state " + state + " country " + country + " postalCode " + postalCode + " knownName " + knownName);
}
}
在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();
}
}