我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
我想从纬度和经度在安卓得到以下值
街道地址 城市/州 邮政编码 完整的地址
如何做到这一点?
当前回答
public static String getAddressFromLatLng(Context context, LatLng latLng) {
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(context, Locale.getDefault());
try {
addresses = geocoder.getFromLocation(latLng.latitude, latLng.longitude, 1);
return addresses.get(0).getAddressLine(0);
} catch (Exception e) {
e.printStackTrace();
return "";
}
}
其他回答
城市和国家不总是在地址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();
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;
}
}
你可以这样做,以获得完整的经纬度地址:
public class MainActivity extends AppCompatActivity {
...
private Geocoder geocoder;
private TextView mAddressTxtVu;
...
// I assume that you got latitude and longitude correctly
mLatitude = 20.23232
mLongitude = 32.999
String errorMessage = "";
geocoder = new Geocoder(context, Locale.getDefault());
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocation(
mlattitude,
mlongitude,
1);
} catch (IOException e) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, e);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " + "Latitude = " + mlattitude +", Longitude = " + mlongitude, illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
// Fetch the address lines using getAddressLine,
// join them, and send them to the thread.
for (int i = 0; i <= address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
// Log.i(TAG, getString(R.string.address_found));
mAddressTxtVu.setText(TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
似乎还没有人提供谷歌Docs (https://developer.android.com/training/location/display-address#java)建议的解决方案。正确的解决方案应该使用IntentService进行反向地理编码的网络调用。
使用意图服务而不是AsyncTask,因为它没有绑定到任何特定的活动。ie。它有自己的生命周期。当地理编码完成时,IntentService将自动停止。
public class GeocodingService extends IntentService {
public GeocodingService() {
super("GeocodingService");
}
@Override
protected void onHandleIntent(@Nullable Intent intent) {
if (intent == null) {
return;
}
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
String errorMessage = "";
BCCDatabase BCCDatabase = skicompanion.skicompanion.storage.BCCDatabase.getInstance(getApplicationContext());
// Get the location passed to this service through an extra.
Location location = intent.getParcelableExtra(
"location");
long trackID = intent.getLongExtra("trackID", -1);
List<Address> addresses = null;
String addressString = "";
try {
addresses = geocoder.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1);
} catch (IOException ioException) {
// Catch network or other I/O problems.
errorMessage = "service not available";
Log.d(Constants.SkiCompanionDebug, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
// Catch invalid latitude or longitude values.
errorMessage = "invalid lat long used";
Log.d(Constants.SkiCompanionDebug, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
// Handle case where no address was found.
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = "no address found";
Log.d(Constants.SkiCompanionDebug, errorMessage);
}
} else {
if(addresses.get(0).getLocality() != null){
addressString += addresses.get(0).getLocality() + ", ";
}
if(addresses.get(0).getAdminArea() != null){
addressString += addresses.get(0).getAdminArea() + ", ";
}
if(addresses.get(0).getCountryName() != null){
addressString += addresses.get(0).getCountryName();
}
//updating DB
BCCDatabase.setTrackLocation(trackID, addressString);
Log.d(Constants.SkiCompanionDebug, "address found: "+ addressString);
}
}
}
从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。