[Android][Google Maps]:获取触摸位置的地址
[Android][Google Maps]: Get the Address of location on touch
我已经在谷歌上搜索了几个小时,但到目前为止没有成功。
我想获取触摸/点击地图位置的地址。
我知道为了获得地址,我需要对坐标进行反向地理编码。但是我如何首先从地图上获取坐标?
Google 地图有回调来做到这一点 like this one or this one。
只需在您的代码中实现它们,一旦它们被触发,只需对坐标进行反向地理编码。您实际上找到了最复杂的部分(您了解需要反向地理编码)。
您需要做的就是设置一个OnMapClickListener, and then the onMapClick()
override will give you a LatLng
object. Then, use a Geocoder对象来获取刚刚点击的点的地址。
在这个简单的示例中,每次用户单击地图上的新点时我都会添加一个标记。
这是您需要的主要功能:
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();
}
//remove previously placed Marker
if (marker != null) {
marker.remove();
}
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
这是我用来测试的完整 class:
public class MapsActivity extends AppCompatActivity {
private GoogleMap mMap;
private LatLng latLng;
private Marker marker;
Geocoder geocoder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
geocoder = new Geocoder(this, Locale.getDefault());
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setMapToolbarEnabled(false);
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();
}
//remove previously placed Marker
if (marker != null) {
marker.remove();
}
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
}
}
在两个不同点点击地图的结果:
我已经在谷歌上搜索了几个小时,但到目前为止没有成功。
我想获取触摸/点击地图位置的地址。
我知道为了获得地址,我需要对坐标进行反向地理编码。但是我如何首先从地图上获取坐标?
Google 地图有回调来做到这一点 like this one or this one。
只需在您的代码中实现它们,一旦它们被触发,只需对坐标进行反向地理编码。您实际上找到了最复杂的部分(您了解需要反向地理编码)。
您需要做的就是设置一个OnMapClickListener, and then the onMapClick()
override will give you a LatLng
object. Then, use a Geocoder对象来获取刚刚点击的点的地址。
在这个简单的示例中,每次用户单击地图上的新点时我都会添加一个标记。
这是您需要的主要功能:
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();
}
//remove previously placed Marker
if (marker != null) {
marker.remove();
}
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
这是我用来测试的完整 class:
public class MapsActivity extends AppCompatActivity {
private GoogleMap mMap;
private LatLng latLng;
private Marker marker;
Geocoder geocoder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
geocoder = new Geocoder(this, Locale.getDefault());
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.getUiSettings().setMapToolbarEnabled(false);
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();
}
//remove previously placed Marker
if (marker != null) {
marker.remove();
}
//place marker where user just clicked
marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
}
}
在两个不同点点击地图的结果: