我正在尝试定位应用,目前我在标记上具有该位置的坐标。现在,如何从已经存在的坐标中获取位置的名称?。
我的代码:
//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 ) );
}
}
这是我的标记
// 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();
}
} );
}
答案 0 :(得分:0)
您可以使用下面的代码在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();
}