【问题标题】:[Android][Google Maps]: Get the Address of location on touch[Android][Google Maps]:一键获取位置地址
【发布时间】:2017-04-02 17:38:35
【问题描述】:

我已经在谷歌上搜索了几个小时,但到目前为止没有运气。

我想获取触摸/点击地图的位置的地址。

我知道为了获得地址,我需要对坐标进行反向地理编码。但是我首先如何从地图中获取坐标呢?

【问题讨论】:

    标签: android google-maps geocoding google-geocoder


    【解决方案1】:

    您需要做的就是设置一个OnMapClickListener,然后onMapClick() 覆盖将为您提供一个LatLng 对象。然后,使用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)));
    
            }
        });
    

    这是我用来测试这个的完整课程:

    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)));
    
                }
            });
    
        }
    }
    

    在两个不同点点击地图的结果:

    【讨论】:

    • 作为一个安卓新手,我真的很感谢你精心制作的答案
    • @potato300:你说得对,答案是一样的,但我知道它更容易理解,我在地铁里,没有可用的 IDE ;)
    • @DanielNugent 我已经尝试使用地理编码器从纬度和经度中获取地址,但它没有提供我当前位置的确切地址,它提供了附近街道的地址或地点。
    【解决方案2】:

    Google Map 有回调函数like this onethis one

    只需在您的代码中实现它们,一旦它们被触发,只需对坐标进行反向地理编码。你实际上找到了最复杂的部分(你明白你需要反向地理编码)。

    【讨论】:

      猜你喜欢
      • 2020-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      相关资源
      最近更新 更多