David
David

Reputation: 910

Recognise click over current location marker in Android Google Maps

In Google Maps V2 for Android, what would be the best option to recognise when the user has clicked the current location marker (blue one with white border)?

I don't get any example in the Internet and the only way I can imagine is this:

googleMap.setOnMapClickListener(new OnMapLongClickListener() {
            @Override
            public void onMapClick(LatLng point) {
                // currentLocation is the location set by onLocationChanged method
                if (currentLocation != null
                            && (currentLocation.getLatitude() == point.latitude
                            && currentLocation.getLongitude() == point.longitude)) {
                        toastIt("Click over current position!");
                    }
            }
        });

Upvotes: 2

Views: 1896

Answers (3)

A.Alqadomi
A.Alqadomi

Reputation: 1589

I know it is late but for any one searching for this It is supported now natively (Since 18 Sept 2017)

from the page mentioned below :

Use the new GoogleMap.OnMyLocationClickListener to detect when the user clicks the My Location blue dot. (Issue 35822305)

@Override
public void onMyLocationClick(@NonNull Location location) {
  Toast.makeText(this, "Current location:\n" + location, Toast.LENGTH_LONG).show();
} 

I suggest to follow this page for the latest updates https://developers.google.com/maps/documentation/android-api/releases

Upvotes: 0

sagus_helgy
sagus_helgy

Reputation: 1437

It's better solution

map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng)
            {
                Location location=getLocation();
                if(map.isMyLocationEnabled()&&location!=null)
                {
                    float distance[]=new float[1];
                    Location.distanceBetween(location.getLatitude(), location.getLongitude(), latLng.latitude, latLng.longitude, distance);
                    if(distance[0]<location.getAccuracy())
                    {
                        Log.d(TAG, "onMapClick: It is user!");
                    }
                }
            }
        });

Upvotes: 0

Niko
Niko

Reputation: 8163

You can add user location marker and take the reference of it's id:

mUserMarkerId = mMap.addMarker(new MarkerOptions().position(latLng)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)))
                        .getId();

And with the onMarkerClickListener you can detect if the marker clicked was the user marker

mMap.setOnMarkerClickListener(new OnMarkerClickListener() {

        @Override
        public boolean onMarkerClick(Marker marker) {
            if (marker.getId().equalsIgnoreCase(mUserMarkerId))
                // Clicked user marker
            }

            return true;
        }
    });

Upvotes: 2

Related Questions