[Android] [Google地图]:获取地点的地址 [英] [Android][Google Maps]: Get the Address of location on touch
问题描述
我想要获取触摸/点击地图的位置地址。
据我所知,为了获得我需要反转地理编码坐标的地址。但是,如何从地图中获取坐标?
解决方案 你需要做的就是设置一个< a href =https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap.OnMapClickListener =noreferrer> OnMapClickListener ,然后 onMapClick()
override会给你一个 LatLng
对象。然后,使用地理编码器对象获取曾经只需点击即可。
在这个简单的示例中,每次用户点击地图上的新点时,我都会添加一个标记。
以下是您需要的主要功能:
mMap.setOnMapClickListener(新的GoogleMap。 OnMapClickListener(){
$ b $ @Override
public void onMapClick(LatLng point){
//保存当前位置
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)+\\\
);
}
Toast.makeText(MapsActivity.this,sb.toString(),Toast.LENGTH_LONG).show();
}
//删除先前放置的标记
if(marker!= null){
marker.remove();
}
//用户点击的位置标记
marker = mMap.addMarker(new MarkerOptions()。position(point).title(Marker)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
以下是我用来测试的完整课程:
公共类MapsActivity扩展AppCompatActivity {
私有GoogleMap mMap;
私人LatLng latLng;
私人标记标记;
地理编码器地理编码器;
@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(){
//做一个空检查来确认我们还没有实例化地图。
if(mMap == null){
//尝试从SupportMapFragment获取映射。
mMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))
.getMap();
//检查我们是否成功获取地图。
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(){
$ b @Override
public void onMapClick(LatLng point){
//保存当前位置
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)+\\\
);
}
Toast.m akeText(MapsActivity.this,sb.toString(),Toast.LENGTH_LONG).show();
}
//删除先前放置的标记
if(marker!= null){
marker.remove();
}
//用户点击的位置标记
marker = mMap.addMarker(new MarkerOptions()。position(point).title(Marker)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)));
}
});
$ b
两次点击地图的结果不同点:
I have been googling this for hours but no luck so far.
I want to get the address of the location where the map is touched / tapped.
I understand that in order to get the address i need to reverse geocode the coordinates. But how do i get the coordinates from the map in the first place?
All you need to do is set up a OnMapClickListener, and then the onMapClick()
override will give you a LatLng
object. Then, use a Geocoder object to get the address of the point that was just clicked on.
In this simple example, I've also added a Marker every time the user clicks a new point on the map.
Here is the main piece of functionality that you need:
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)));
}
});
Here is the full class that I used to test this:
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)));
}
});
}
}
Result of tapping the map in two different points:
这篇关于[Android] [Google地图]:获取地点的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!