【问题标题】:Android Google Map v2 - move marker to clicked position and update geo coordinatesAndroid Google Map v2 - 将标记移动到点击位置并更新地理坐标
【发布时间】:2014-08-20 21:38:27
【问题描述】:

在我的应用中,我会自动检测用户的当前位置并将地图以标记为中心。

我想让用户能够点击地图上的其他地方,并让标记出现在他们点击的位置,并将纬度/经度更新到该新位置。

我该怎么做

【问题讨论】:

标签: android google-maps


【解决方案1】:

试试这个

 Marker marker;
 GoogleMap mMap;

 mMap.setOnMapClickListener(new OnMapClickListener() {

        @Override
        public void onMapClick(LatLng latlng) {
            // TODO Auto-generated method stub

            if (marker != null) {
                marker.remove();
            }
            marker = mMap.addMarker(new MarkerOptions()
                    .position(latlng)
                    .icon(BitmapDescriptorFactory
                            .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
            System.out.println(latlng);

       }
   });

【讨论】:

  • 这行得通,但表示我的位置的蓝点仍然在我的位置。而新的标记不是一个蓝点,而是一个倒置的红色水滴?
【解决方案2】:

要使用当前位置添加标记,您必须实现 LocationListener

使用以下代码,您可以添加带有您的位置的标记并移动相机:

            public void onLocationChanged(Location location) {

            map.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude()))
                    .title("My Location"));

            /* ..and Animate camera to center on that location !
             * (the reason for we created this custom Location Source !) */
            map.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(), location.getLongitude())));
        }

为了在用户点击地图时添加制造商,您可以使用OnMapLongClickListener

    @Override
    public void onMapLongClick(LatLng point) {

        mMap.addMarker(new MarkerOptions()
            .position(point)
            .snippet(""));
    }

【讨论】:

猜你喜欢
  • 2015-09-26
  • 2013-08-07
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-11
  • 2013-05-11
相关资源
最近更新 更多