我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
当前回答
Geocoder geocoder =new Geocoder(mContext, Locale.getDefault());
// Get the current location from the input parameter list
Location loc = params[0];
// Create a list to contain the result address
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(loc.getLatitude(),
loc.getLongitude(), 10);
} catch (IOException e1) {
Log.e("LocationSampleActivity","IO Exception in getFromLocation()");
e1.printStackTrace();
} catch (IllegalArgumentException e2) {
// Error message to post in the log
String errorString = "Illegal arguments " +
Double.toString(loc.getLatitude()) +
" , " +
Double.toString(loc.getLongitude()) +
" passed to address service";
Log.e("LocationSampleActivity", errorString);
e2.printStackTrace();
}
Address address=null;
String zip=null;
String city=null;
String state=null;
StringBuffer st=new StringBuffer();
// If the reverse geocode returned an address
if (addresses != null && addresses.size() > 0) {
String add=addresses.get(0).getAddressLine(0)+","
+addresses.get(0).getSubAdminArea()+","
+addresses.get(0).getSubLocality();
city=addresses.get(0).getLocality();
state=addresses.get(0).getAdminArea();
// Get the first address
for(int i=0 ;i<addresses.size();i++){
address = addresses.get(i);
if(address.getPostalCode()!=null){
zip=address.getPostalCode();
break;
}
}
其他回答
您正在寻找“地理编码”一词。
你需要做的是:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
要做更多,你应该在这里阅读Geocoder。
试试这段代码(正在工作)
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);
}
}
城市和国家不总是在地址1号线和2号线…
例子在这里
So,
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude,longitude, 1);
String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String zip = addresses.get(0).getPostalCode();
String country = addresses.get(0).getCountryName();
用这个对我有用: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选项是正确答案
试试这个,我的朋友
private String getCompleteAddressString(double LATITUDE, double LONGITUDE) {
String strAdd = "";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i <= returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address", strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Canont get Address!");
}
return strAdd;
}