[英]Get the address from the coordinates Google Map Android
I'm trying a location app, currently I have the coordinates of that location on the marker. 我正在尝试使用位置信息应用,目前我在标记上具有该位置的坐标。 Now, how can I get the name of the location from the coordinates already there?.
现在,如何从已经存在的坐标中获取位置名称?
My code: 我的代码:
//search add address
private void geoLocate() {
Log.d( TAG, "geoLocate: geolocating" );
String searchString = mSearchText.getText().toString();
Geocoder geocoder = new Geocoder( MapActivity.this );
List<Address> list = new ArrayList<>();
try {
list = geocoder.getFromLocationName( searchString, 1 );
} catch (IOException e) {
Log.e( TAG, "geoLocate: IOException" + e.getMessage() );
}
if (list.size() > 0) {
Address address = list.get( 0 );
Log.d( TAG, "geoLocate: found a location: " + address.toString() );
moveCamera( new LatLng( address.getLatitude(), address.getLongitude() ), DEFAULT_ZOOM,address.getAddressLine( 0 ) );
}
}
This is my marker 这是我的标记
// add marker
public void addMarkers() {
mMap.setOnMapClickListener( new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng latLng) {
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position( latLng );
markerOptions.title( latLng.latitude + " : " + latLng.longitude );
mMap.clear();
mMap.animateCamera( CameraUpdateFactory.newLatLng( latLng ) );
mMap.addMarker( markerOptions );
shareView();
}
} );
}
You can use bellow code to fetch address base on latlong 您可以使用以下代码在latlong上获取地址
try {
Geocoder geocoder;
String city=null;
List<Address> yourAddresses;
geocoder = new Geocoder(this, Locale.getDefault());
yourAddresses = geocoder.getFromLocation(yourLatitude, yourLongitude, 1);
final String yourAddress = yourAddresses.get(0).getAddressLine(0);
if (yourAddresses != null && yourAddresses.size() > 0) {
android.location.Address address = yourAddresses.get(0);
@SuppressWarnings("MismatchedQueryAndUpdateOfStringBuilder") StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
sb.append(address.getAddressLine(i)).append("\n");
}
city = address.getLocality();
Log.d("city", "onCreate: "+city);
}
} catch (IOException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.