2

I have a Google Maps API Activity and want the markes only to be clickable if the user is in a certain radius to them. How is the best way to do this? This is how I set my markers:

Marker marker1 = googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(49.793012, 9.926201))
            .title(getString(R.string.Title1))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
    Marker marker2 = googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(49.792742, 9.939118))
            .title(getString(R.string.Title2))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
    Marker marker3 = googleMap.addMarker(new MarkerOptions()
            .position(new LatLng(49.793349, 9.932558))
            .title(getString(R.string.Title3))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));

Solution code:

LatLng markerPosition = marker.getPosition();
myLocation = locationManager.getLastKnownLocation(provider);
Location markerLoc = new Location("marker");
markerLoc.setLatitude(markerPosition.latitude);
markerLoc.setLongitude(markerPosition.longitude);
float meters = myLocation.distanceTo(markerLoc);
maidi
  • 3,219
  • 6
  • 27
  • 55
  • Did you solve your problem? Have you cheked my answer? – Skizo-ozᴉʞS ツ Jun 11 '15 at 19:00
  • I still had problems comparing the two locations. I did it this way now: `LatLng markerPosition = marker.getPosition(); myLocation = locationManager.getLastKnownLocation(provider); Location markerLoc = new Location("marker"); markerLoc.setLatitude(markerPosition.latitude); markerLoc.setLongitude(markerPosition.longitude); float meters = myLocation.distanceTo(markerLoc);` – maidi Jun 12 '15 at 20:35
  • Edit your quesiton with your code – Skizo-ozᴉʞS ツ Jun 13 '15 at 11:52

1 Answers1

0

You can get the LatLng of the markers by marker.getPosition(); then you can compare it with the CurrentLocation. To do this, you can check this post, and next you can make clickable or notclickable a marker as follows:

getMap().setOnMarkerClickListener(new OnMarkerClickListener() {
    public boolean onMarkerClick(Marker marker) {
        onMapClick(marker.getPosition());
        return true;
    }
});

If you return true for the function, it means that you've accepted the occurred click event on your marker; otherwise, you've not accepted it.

Moreover, you can do it with:

MarkerOptions.clickable and Marker.setClickable.

This is just a guide who how I would do what you want. hope it helps. :)

Community
  • 1
  • 1
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148