Search code examples
javaandroidgoogle-mapsgoogle-maps-markersandroid-geofence

Adding multiple Geofence to a map


So far I'm able to add single geofence to the map with help of this tutorial. Actually I'm adding markers to the map on map click event allowing me to add multiple markers on touch. I wan't to add geofence to all the markers that I'm adding. I've tried making few changes in particular methods of the above tutorial as

ArrayList<LatLng> locs= new ArrayList<LatLng>();
    // Create a marker for the geofence creation
private void markerForGeofence(LatLng latLng) {
    Log.i(TAG, "markerForGeofence(" + latLng + ")");
    String title = latLng.latitude + ", " + latLng.longitude;
    // Define marker options
    MarkerOptions markerOptions = new MarkerOptions()
            .position(latLng)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE))
            .title(title);
    if (map != null) {
        // Remove last geoFenceMarker
      //  if (geoFenceMarker != null)
      //      geoFenceMarker.remove();

        geoFenceMarker = map.addMarker(markerOptions);  
        gfmarkr.add(geoFenceMarker);
    }
}
...
   // Start Geofence creation process
private void startGeofence() {
    Log.i(TAG, "startGeofence()");
    if (geoFenceMarker != null) {
        for (int i = 0; i < gfmarkr.size(); i++) {
            Geofence geofence = createGeofence(locs.get(i), GEOFENCE_RADIUS);
            GeofencingRequest geofenceRequest = createGeofenceRequest(geofence);
            addGeofence(geofenceRequest);
        }
    } else {
        Log.e(TAG, "Geofence marker is null");
    }
}

In short what is the method of adding more than one geofence. Any solution will be of great help. Thankyou.


Solution

  • It is solved. I passed locs.get(i) to all the methods that are responsible for creating a particular Geofence and used it as a point of reference for position. Also, I removed the statements

     if (geoFenceLimits != null)
        geoFenceLimits.remove();
    

    from drawGeofence() method.