简体   繁体   中英

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!");
                    }
            }
        });

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;
        }
    });

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!");
                    }
                }
            }
        });

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM