user2219097
user2219097

Reputation: 357

Android Google Map how to check if user is in marker circle region

I am using Google Maps, and want to be able to detect if a user is in the radius of a Marker (placed on map), using the users current location. I have the users current location coordinates (latitude and longitude) and the marker's coordinates, but do not know how to calculate whether the user is in the area. The picture below might best describe what I want to do.

enter image description here

I have an algorithm like this:

map.OnInfoWindowClickListener(new OnInfoWindowClickListener()){
    public void isUserInArea(Marker marker){

        float[] distance = new float[2];
           //users current location 
        Location.distanceBetween(currentLocation.getLatitude(),currentLocatiocation.getLongitude(),
        marker.getPosition().latitude, marker.getPosition().longitude, distance);
}

Which doesn't find if the current location is within marker region because I can't get hold of the markers circle. It's the closest i've got to it however. Would appreciate help.

To help, I add a circle to a map like this

// adding circle to map
circleOptions = new CircleOptions();
circleOptions.center(new LatLng(locationlist.get(i)
        .getLatitude(), locationlist.get(i).getLongitude()));
circleOptions.radius(20);
circleOptions.strokeColor(Color.BLUE);
circleOptions.fillColor(Color.BLUE);
map.addCircle(circleOptions);

//check that all tasks are in circle radius

Marker locationMarker = map.addMarker(markerOp);
locationMarker.showInfoWindow();

Upvotes: 3

Views: 10597

Answers (3)

Rameshbabu
Rameshbabu

Reputation: 611

This tutorial can help your requirement. User entry exit proximity alert

all the best.

Upvotes: 1

alvaro.delaserna
alvaro.delaserna

Reputation: 549

Why don't you use proximity alerts?

This is what you can do: instead of drawing a circle, place a Marker on the map like so:

map.addMarker(new MarkerOptions()
    .title(name)
    .icon(BitmapDescriptorFactory.fromResource(R.drawable.gpsmap))
    .position(coordinates)
    .flat(true)
    .rotation(90));

and then add a proximity alert to it calling the following method:

// Proximity alert
private void addProximityAlert(double lat, double lng, int id){
    Intent intent = new Intent(PROX_ALERT_INTENT);
    PendingIntent proximityIntent = PendingIntent.getBroadcast(this, id, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    locationManager.addProximityAlert(lat, lng, POINT_RADIUS, EXPIRATION, proximityIntent);
}

where EXPIRATION = -1 (so the proximity alert doesn't expire) and POINT_RADIUS takes the values of the radius of your previous circle.

Then you need to add a proximity alert listener to the map:

IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);

where PROX_ALERT_INTENT = "yourProjectPackageName.ProximityActivity";

Hope this helps

Upvotes: 2

Natalie Carr
Natalie Carr

Reputation: 3797

I use this:

Location.distanceBetween(mMarker.getPosition().latitude,
            mMarker.getPosition().longitude, mCircle.getCenter().latitude,
            mCircle.getCenter().longitude, distance);

if (distance[0] > mCircle.getRadius()) {
      //Do what you need

}else if (distance[0] < mCircle.getRadius()) {
//Do what you need
}

Upvotes: 12

Related Questions