Skip to content Skip to sidebar Skip to footer

How To Constantly Detect Nearby Marker Locations From Current Location In Google Maps Android?

I am trying to display a TextView at the top left corner of Google Maps that inform users if they are x distances from particular marker point locations, i.e. 'You are close to loc

Solution 1:

Short answer location.distanceTo(anotherLocation)

In setupMap i recommend to add additional listener, to get all location changes in a simple way.

mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(this);

Then in this listener you just take distance between current location and your targets. Distance in meters.

@Override
public void onMyLocationChange(Location location) {
    Location target = new Location("target");
    for(LatLng point : new LatLng[]{POINTA, POINTB, POINTC, POINTD}) {
        target.setLatitude(point.latitude);
        target.setLongitude(point.longitude);
        if(location.distanceTo(target) < METERS_100) {
            // bingo!
        }
    }
}

Of course, you should make your activity implements GoogleMap.OnMyLocationChangeListener


Post a Comment for "How To Constantly Detect Nearby Marker Locations From Current Location In Google Maps Android?"