我想獲取點擊地圖的地址。我嘗試使用地理編碼器。但它不工作。這是否需要任何API。請幫助我。我將在下面給我使用的代碼發短信。從經緯度獲取地址
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
//save current location
latLng = point;
List<Address> addresses = new ArrayList<>();
try {
addresses = geocoder.getFromLocation(point.latitude, point.longitude,1);
} catch (IOException e) {
e.printStackTrace();
}
android.location.Address address = addresses.get(0);
if (address != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
sb.append(address.getAddressLine(i) + "\n");
}
Toast.makeText(MapsActivity.this, sb.toString(), Toast.LENGTH_LONG).show();
}
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")));
}
});
查看我的答案:http://stackoverflow.com/a/32647204/3626214 – Aspicas
openstreetmap.org提供了一種類似的功能來反轉地理編碼。 –
define:*但它不工作。* ...這個代碼極其愚蠢......'addresses = new ArrayList <>();'???對於下一行是'addresses = geocoder ....'...'addresses.get(0);'?如果地址是空的呢? ...爲什麼它不在'地址= geocoder ....後面'第一次嘗試? – Selvin