Maps V2 myLocation blue dot callback

北慕城南 提交于 2019-12-01 12:00:06

问题


I want to be able to click the blue dot (my location) that is shown on the map. Is there anyway to get a callback from that click?

Thanks, Martijn


回答1:


One possible workaround might be drawing a Marker (with a similar icon) on top of the My Location dot so you can receive the corresponding onMarkerClick() callback. This would also require removing the marker and adding it to the new location everytime there's a location update event, which you can listen to by implementing OnMyLocationChangeListener.

EDIT: the OnMyLocationChangeListener interface is now deprecated, one should use instead the new LocationClient and the associated LocationListener.

So the relevant code could look something like this (I hadn't actually tested this):

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 ... */
        }
    }
}


来源:https://stackoverflow.com/questions/16047988/maps-v2-mylocation-blue-dot-callback

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!