Skip to content Skip to sidebar Skip to footer

Maps V2 Mylocation Blue Dot Callback

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

Solution 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):

publicclassDemoMapFragmentextendsSupportMapFragmentimplementsOnMyLocationChangeListener, OnMarkerClickListener { 

    // Note that 'mMap' may be null if the Google Play services APK is not available. privateGoogleMap mMap; 
    privateMarker myLocationMarker;
    privatestaticBitmapDescriptor markerIconBitmapDescriptor;
    /* ... */@OverridepublicvoidonResume() { 
        super.onResume(); 
        setUpMapIfNeeded(); // Get a reference to the map      
        mMap.setMyLocationEnabled(true); // Enable the my-location layer 
        mMap.setOnMyLocationChangeListener(this); 
        mMap.setOnMarkerClickListener(this);
    }

    privatevoidsetUpMapIfNeeded() { 
        // 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(newMarkerOptions() 
                        .position(newLatLng(mMap.getMyLocation().getLatitude(),mMap.getMyLocation().getLongitude())) 
                        .icon(markerIconBitmapDescriptor)); 

                // Set default zoom 
                mMap.moveCamera(CameraUpdateFactory.zoomTo(15f)); 
            } 
        } 
    }   

    @OverridepublicvoidonMyLocationChange(Location location) {
        // Remove the old marker object
        myLocationMarker.remove(); 

        // Add a new marker object at the new (My Location dot) location
        myLocationMarker = mMap.addMarker(newMarkerOptions() 
                .position(newLatLng(location().getLatitude(),location().getLongitude())) 
                .icon(markerIconBitmapDescriptor)); 
    }

    @OverridepublicbooleanonMarkerClick(Marker marker) {
        if (marker.equals(myLocationMarker)) {
            /* My Location dot callback ... */
        }
    }
}

Post a Comment for "Maps V2 Mylocation Blue Dot Callback"