3

I am trying to implement a Geofencing system using polygon shape. Basically if an user enters the geofencing region, the user should get a notification. After going through many research i was only able to find Geofence using Circular. So far i implemented the system but it only monitors by circular shape not if someone enters the polygon drawn in the map. If any has done Polygon Geonfencing before please help me out

This is the code i used to draw my polygon

    private void drawGeofence() {
    Log.d(TAG, "drawGeofence()");

    polyLatLng = new ArrayList<>(  );
    polyLatLng.add( new LatLng( 6.895450, 79.852170 ) ); // Should match last point
    polyLatLng.add( new LatLng(6.897287, 79.859544));
    polyLatLng.add( new LatLng( 6.905271, 79.862609 ) );
    polyLatLng.add( new LatLng( 6.906114, 79.858998 ) );
    polyLatLng.add( new LatLng( 6.911808, 79.856206 ) );
    polyLatLng.add( new LatLng( 6.912200, 79.851381 ) );
    polyLatLng.add( new LatLng( 6.911627, 79.849621 ) );
    polyLatLng.add( new LatLng( 6.910965, 79.848073 ) );
    polyLatLng.add( new LatLng( 6.895450, 79.852170 ) );              // Should match first point

    Log.i(TAG, "computeArea " + SphericalUtil.computeArea(polyLatLng));

    map.addPolygon(new PolygonOptions()
            .addAll(polyLatLng)
            .strokeColor(Color.BLACK)
            .strokeWidth( 4 )
            .fillColor(0x220000FF));
}

Here's my geofence code which track only in circular region

 private static final float GEOFENCE_RADIUS = 1006.3975694699f; // in meters
private void startGeofence() {
    Log.i(TAG, "startGeofence()");
    if( geoFenceMarker != null ) {

        // create geofence
        Geofence geofence = createGeofence( 6.904254, 79.853798, GEOFENCE_RADIUS );
        GeofencingRequest geofenceRequest = createGeofenceRequest( geofence );
        addGeofence( geofenceRequest );
    } else {
        Log.e(TAG, "Geofence marker is null");
    }
}

// Create a Geofence
private Geofence createGeofence( double lat, double lng, float radius ) {
    Log.d(TAG, "createGeofence");

    return new Geofence.Builder()
            .setRequestId(GEOFENCE_REQ_ID)
            .setCircularRegion( lat, lng, radius)
            .setExpirationDuration( GEO_DURATION )
            .setTransitionTypes( Geofence.GEOFENCE_TRANSITION_ENTER
                    | Geofence.GEOFENCE_TRANSITION_EXIT )
            .build();
}enter code here
  • Possible duplicate of [Android: Build Polygonal Shape Geofence](https://stackoverflow.com/questions/31847977/android-build-polygonal-shape-geofence) – Marian Paździoch Nov 16 '18 at 09:13
  • I tried that way too sir but i was not able to use that code inside my project. If you can help me i would really appreciate it. Thank you – Waleedh Naim Nov 16 '18 at 09:22
  • "geofences/location services are inaccurate enough to not even be able to alert about circles, don't even think about some other concrete shape" – Marian Paździoch Nov 16 '18 at 09:27
  • @WaleedhNaim What did you end up implementing for this? The linked post in the comments ("Android: Build Polygonal Shape Geofence") is only possibly only suitable for simple polygonal shapes. I think there must be a way to compose a polygonal shape internally of overlapping circles. – Dom Jun 16 '19 at 13:15

0 Answers0