Create and Monitor geofences using polygon shape

Viewed 1103

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
0 Answers
Related