【问题标题】:Maps V2 myLocation blue dot callbackMaps V2 myLocation 蓝点回调
【发布时间】:2013-04-16 22:06:47
【问题描述】:

我希望能够点击地图上显示的蓝点(我的位置)。反正有没有从该点击中获得回调?

谢谢, 马丁

【问题讨论】:

    标签: android google-maps map android-maps-v2 mylocationoverlay


    【解决方案1】:

    一种可能的解决方法是在“我的位置”点顶部绘制一个Marker(带有类似图标),以便您可以接收相应的onMarkerClick() 回调。这还需要在每次发生位置更新事件时移除标记并将其添加到新位置,您可以通过实现OnMyLocationChangeListener 来收听。

    编辑OnMyLocationChangeListener 接口现已弃用,应使用新的LocationClient 和关联的LocationListener

    所以相关代码可能看起来像这样(我没有实际测试过):

    public class DemoMapFragment extends SupportMapFragment implements OnMyLocationChangeListener, OnMarkerClickListener { 
    
        // Note that 'mMap' may be null if the Google Play services APK is not available. 
        private GoogleMap mMap; 
        private Marker myLocationMarker;
        private static BitmapDescriptor markerIconBitmapDescriptor;
        /* ... */
    
        @Override 
        public void onResume() { 
            super.onResume(); 
            setUpMapIfNeeded(); // Get a reference to the map      
            mMap.setMyLocationEnabled(true); // Enable the my-location layer 
            mMap.setOnMyLocationChangeListener(this); 
            mMap.setOnMarkerClickListener(this);
        }
    
        private void setUpMapIfNeeded() { 
            // Do a null check to confirm that we have not already instantiated the map. 
            if (mMap == null) { 
                mMap = getMap(); 
                // Check if we were successful in obtaining the map. 
                if (mMap != null) { 
                    // The Map is verified. It is now safe to manipulate the map:
    
                    // Load custom marker icon
                    markerIconBitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.my_location_dot_icon); 
    
                    // When the map is first loaded we need to add our marker on top of My Location dot
                    myLocationMarker = mMap.addMarker(new MarkerOptions() 
                            .position(new LatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude())) 
                            .icon(markerIconBitmapDescriptor)); 
    
                    // Set default zoom 
                    mMap.moveCamera(CameraUpdateFactory.zoomTo(15f)); 
                } 
            } 
        }   
    
        @Override
        public void onMyLocationChange(Location location) {
            // Remove the old marker object
            myLocationMarker.remove(); 
    
            // Add a new marker object at the new (My Location dot) location
            myLocationMarker = mMap.addMarker(new MarkerOptions() 
                    .position(new LatLng(location().getLatitude(),location().getLongitude())) 
                    .icon(markerIconBitmapDescriptor)); 
        }
    
        @Override
        public boolean onMarkerClick(Marker marker) {
            if (marker.equals(myLocationMarker)) {
                /* My Location dot callback ... */
            }
        }
    }
    

    【讨论】:

    • 感谢您的回复。我猜这是一个很好的工作,我已经实现了它。我只是不明白为什么api不提供这个,谁不想点击myLocation?!我也理解 V1 api 确实提供了它(虽然我从未使用过 V1 地图)。
    • @Tinus81 很高兴它对你有用。我想这个功能会在未来添加release
    • 顺便说一句,我为此制作了一个完全透明的自定义标记。用户不会注意到地图应用的不同之处!
    猜你喜欢
    • 1970-01-01
    • 2015-09-25
    • 1970-01-01
    • 2012-12-05
    • 2013-01-27
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多