我想获取点击地图的地址。我尝试使用地理编码器。但它不工作。这是否需要任何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