I took over a project recently and already implemented a set of codes to calculate the geofence area and draw. I have difficulty in understand this set of codes. Could anyone explain to me, how is the geofence being calculated; what is the diameter, what coordinates are needed to draw etc?
Also, is there any guide to which I could refer?
static final double EARTH_RADIUS_X = 6378137.0;
static final double EARTH_RADIUS_Y = 6356752.3142;
final List< Polygon > mGeoFences = new ArrayList<>();
final List< Point > mLatLngs = new ArrayList<>();
void drawGeoFence() {
final GraphicsOverlay overlay = map_view.getGraphicsOverlays().get( 0 );
for( final Point point : mLatLngs ) {
mMap.addPointToDetermine1stTimeZoom( point );
final double lat = point.getY();
final double lng = point.getX();
mGeoFences.add( drawCircle( overlay.getGraphics(), mThreshold, lat, lng, false ) );
drawCircle( overlay.getGraphics(), 3, lat, lng, true ); // center point
}
}
Polygon drawCircle( final List< Graphic > graphics, final float radius, final double lat, final double lng, boolean isCenter ) {
final Point point = new Point( lng, lat, SpatialReferences.getWgs84() );
final PointCollection c = new PointCollection( SpatialReferences.getWgs84() );
final double centerX = point.getX();
final double centerY = point.getY();
final int pointsCount = 360;
final double slice = Math.PI / 180.0;
final double dX = radius / EARTH_RADIUS_X / slice;
final double dY = radius / ( EARTH_RADIUS_Y * Math.cos( lat * slice ) ) / slice;
for( int i = 0; i <= pointsCount; i++ ) {
final double r = slice * i;
final double x = centerX + dX * Math.cos( r );
final double y = centerY + dY * Math.sin( r );
c.add( x, y );
}
final Polygon polygon;
try {
polygon = new Polygon( c );
} catch( final Throwable e ) {
AdvancedLog.e( e );
PopUp.showErrorMsgDialog( this, PopUp.getListenerFinishActivity( this ), R.string.msg_invalid_asset_location_data );
return null;
}
final SimpleFillSymbol fillSymbols = new SimpleFillSymbol( SimpleFillSymbol.Style.SOLID,
isCenter ? Color.RED : red_transparent, null );
graphics.add( new Graphic( polygon, fillSymbols ) );
final Polyline polyline = new Polyline( c );
final SimpleLineSymbol lineSymbol = new SimpleLineSymbol( SimpleLineSymbol.Style.SOLID, Color.RED, 1 );
graphics.add( new Graphic( polyline, lineSymbol ) );
graphics.add( new Graphic( point ) );
return polygon;
}