Skip to content

Mappls GeoFence View

Introduction

A ready to use UI Widget to create/edit GeoFence in an Android application.

Add the dependency

Add below dependency in app level build.gradle file

Kotlin (build.gradle.kts)

// When using the BoM, you don't specify versions in Mappls library dependencies
implementation("com.mappls.sdk:geofence-ui")

//Or Add Dependency with Version
implementation("com.mappls.sdk:geofence-ui:2.0.0")

Groovy (build.gradle)

// When using the BoM, you don't specify versions in Mappls library dependencies
implementation 'com.mappls.sdk:geofence-ui'

//Or Add Dependency with Version
implementation("com.mappls.sdk:geofence-ui:2.0.0")

Add GeoFence View to your application

You can add GeoFence view in two ways

  1. Using XML
  2. Using Programatically

Using XML

<com.mappls.sdk.geofence.ui.views.GeoFenceView  
  android:id="@+id/geo_fence_view" 
  android:layout_width="match_parent"  
  android:layout_height="match_parent" />

We can set the following properties:

  1. mappls_geoFence_circleButtonDrawable: To change the circle Button Drawable

  2. mappls_geoFence_polygonButtonDrawable: To change the polygon selector button drawable

  3. mappls_geoFence_circleFillColor: To change Circle Fill colors

  4. mappls_geoFence_circleFillOutlineColor: To change circle Outline color

  5. mappls_geoFence_circleCenterDrawable: To change the image of the center of circle

  6. mappls_geoFence_handToolDrawable: To change action button icon which is visible when polygon is selected

  7. mappls_geoFence_polygonMidPointDrawable: To change midpoint icon of the polygon boundaries

  8. mappls_geoFence_polygonEdgeDrawable: To change polygon corners icon

  9. mappls_geoFence_polygonIntersectionDrawable: To change polygon intersection point icon of polygon

  10. mappls_geoFence_polygonFillColor: To change fill color of polygon

  11. mappls_geoFence_polygonFillOutlineColor: To change outline color of polygon

  12. mappls_geoFence_seekBarThumbDrawable: To change seekbar thumb icon

  13. mappls_geoFence_seekBarProgressPrimaryColor: To change seekbar Progress bar primary color

  14. mappls_geoFence_seekBarProgressSecondaryColor: To change seekbar Progress bar secondary color

  15. mappls_geoFence_seekBarProgressRadius: To change seekbar corner radius

  16. mappls_geoFence_deleteButtonDrawable: To change drawable of Delete button icon

  17. mappls_geoFence_showSeekbar: If its value is false than it hides the seekbar and if its true than it show seekbar

  18. mappls_geoFence_showToolView: If its value is false than it hides the geofence shape toggle buttons and if its true than it show geofence shape toggle buttons

  19. mappls_geoFence_showActionButton: If its value is false than it hides the polygon draw enable/disable button and if its true than it show polygon draw enable/disable button

  20. mappls_geoFence_showUI: If its value is false than it hide all control buttons and if its true than it show all control buttons

  21. mappls_geoFence_showDeleteButton: If its value is false than it hides polygon delete button.
  22. mappls_geoFence_polygonDrawingBackgroundColor: To change the color of Polygon drawing board color.
  23. mappls_geoFence_showPolygonMidPointIcon: To hide polygon mid points icon
  24. mappls_geoFence_polygonOutlineWidth: To set the polygon outline width
  25. mappls_geoFence_circleOutlineWidth: To change the circle outline width
  26. mappls_geoFence_polygonDrawingLineColor: To set the polygon sketch drawing line.
  27. mappls_geoFence_draggingLineColor: To change the dragging line of Polygon edges and circle radius changing line.
  28. mappls_geoFence_minRadius: To set minimum radius of circle.
  29. mappls_geoFence_maxRadius: To set maximum radius of circle
  30. mappls_geoFence_radiusInterval: To set the step size of radius changing
  31. mappls_geoFence_showPolygonCentreIcon: To show/hide centre point of the Polygon
  32. mappls_geoFence_polygonCentreDrawable: To set the drawable for centre point of the Polygon

Using Programatically

Kotlin

//To create with default ui and styles
geoFenceView = GeoFenceView(this)

//Or If you want to change the properties of view
geoFenceView = GeoFenceView(this, GeoFenceOptions.builder().showUI(false).build())

Java

//To create with default ui and styles
geoFenceView = new GeoFenceView(this)

//Or If you want to change the properties of view
geoFenceView = new GeoFenceView(this, GeoFenceOptions.builder().showUI(false).build())

GeoFenceOptions has following methods to change the Properties:

  1. cicleButtonDrawable(int): To change the circle Button drawable

  2. polygonButtonDrawable(int): To change the polygon selector button drawable

  3. circleFillColor(int): To change Circle Fill colors

  4. circleFillOutlineColor(int): To change circle Outline color

  5. circleCentreDrawable(int): To change the image of the center of circle

  6. actionButtonDrawable(int): To change action button icon which is visible when polygon is selected

  7. polygonMidPointDrawable(int): To change midpoint icon of the polygon boundaries

  8. polygonEdgeDrawable(int): To change polygon corners icon

  9. polygonIntersectionDrawable(int): To change polygon intersection point icon of polygon

  10. polygonFillColor(int): To change fill color of polygon

  11. polygonFillOutlineColor(int): To change outline color of polygon

  12. seekbarThumbDrawable(int): To change seekbar thumb icon

  13. seekbarPrimaryColor(int): To change seekbar Progress bar primary color

  14. seekbarSecondaryColor(int): To change seekbar Progress bar secondary color

  15. seekbarCornerRadius(int): To change seekbar corner radius

  16. deleteButtonDrawable(int): To change drawable of Delete button icon

  17. showSeekBar(boolean): If its value is false than it hides the seekbar and if its true than it show seekbar

  18. showToolsButton(boolean): If its value is false than it hides the geofence shape toggle buttons and if its true than it show geofence shape toggle buttons

  19. showActionButton(boolean): If its value is false than it hides the polygon draw enable/disable button and if its true than it show polygon draw enable/disable button

  20. showUI(boolean): If its value is false than it hide all control buttons and if its true than it show all control buttons

  21. showDeleteButton(boolean): If its value is false than it hides polygon delete button.
  22. polygonDrawingBackgroundColor(int): To change the color of Polygon drawing board color.
  23. showPolygonMidPointIcon(boolean): To hide polygon mid points icon
  24. polygonOutlineWidth(Float): To set the polygon outline width
  25. circleOutlineWidth(Float): To change the circle outline width
  26. polygonDrawingLineColor(int): To set the polygon sketch drawing line.
  27. draggingLineColor(int): To change the dragging line of Polygon edges and circle radius changing line.
  28. minRadius(int): To set minimum radius of circle.
  29. maxRadius(int): To set maximum radius of circle
  30. radiusInterval(int): To set the step size of radius changing
  31. showPolygonCentreIcon(boolean): To show/hide centre point of the Polygon
  32. polygonCentreDrawable(int): To set the drawable for centre point of the Polygon

Initialise geofence view

Kotlin

override fun onCreate(savedInstanceState: Bundle?) {  
    super.onCreate(savedInstanceState)
    //..
    mBinding.geoFenceView.onCreate(savedInstanceState)  
    mBinding.geoFenceView.setGeoFenceViewCallback(this)  
    val geoFence = GeoFence()  
    geoFence.geoFenceType = GeoFenceType.CIRCLE  
    geoFence.circleCenter = LatLng(25.4358, 81.8463)  
    geoFence.circleRadius = 200  
    mBinding.geoFenceView.geoFence = geoFence
    //..

} 

override fun onResume() {  
    super.onResume()  
    mBinding.geoFenceView.onResume()  
}  

override fun onStop() {  
    super.onStop()  
    mBinding.geoFenceView.onStop()  
}  

override fun onStart() {  
    super.onStart()  
    mBinding.geoFenceView.onStart()  
}  

override fun onLowMemory() {  
    super.onLowMemory()  
    mBinding.geoFenceView.onLowMemory()  
}  

override fun onDestroy() {  
    super.onDestroy()  
    mBinding.geoFenceView.onDestroy()  
}  

override fun onSaveInstanceState(outState: Bundle) {  
    super.onSaveInstanceState(outState)  
    mBinding.geoFenceView.onSaveInstanceState(outState)  
}

Java

@Override
protected void onCreate(Bundle savedInstanceState) {
    //..
    mBinding.geoFenceView.onCreate(savedInstanceState);
    mBinding.geoFenceView.setGeoFenceViewCallback(this);
    GeoFence geoFence = new GeoFence();
    geoFence.setGeoFenceType(GeoFenceType.CIRCLE);
    geoFence.setCircleCenter(new LatLng(25.4358, 81.8463));
    geoFence.setCircleRadius(200);
    mBinding.geoFenceView.setGeoFence(geoFence);
    //..
}

@Override
protected void onStart() {
    super.onStart();
    mBinding.geoFenceView.onStart();
}

@Override
protected void onResume() {
    super.onResume();
    mBinding.geoFenceView.onResume();
}

@Override
protected void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);
    mBinding.geoFenceView.onSaveInstanceState(outState);
}

@Override
protected void onPause() {
    super.onPause();
    mBinding.geoFenceView.onPause();
}

@Override
protected void onStop() {
    super.onStop();
    mBinding.geoFenceView.onStop();
}

@Override
protected void onDestroy() {
    super.onDestroy();
    mBinding.geoFenceView.onDestroy();
}

Callbacks getting from GeoFence View

Implement from GeoFenceViewCallback interface 1. void onGeoFenceReady(MapplsMap mapplsMap): When map is loaded successfully 2. void geoFenceType(GeoFenceType geofenceType): When toggle between geofence type 3. void onCircleRadiusChanging(int radius): When radius of the circle changes 5. void onUpdateGeoFence(GeoFence geoFence): On update geofence data 6. void hasIntersectionPoints(): When Polygon is intersecting

To change the circle Radius

On circle radius changing:

Kotlin

geoFenceView.onRadiusChange(progress)

Java

geoFenceView.onRadiusChange(progress);

On circle radius change finish

Kotlin

geoFenceView.radiusChangeFinish(seekBar?.progress!!)

Java

geoFenceView.radiusChangeFinish(seekBar.getProgress());

Toggle draw polygon enable/disable

Kotlin

geoFenceView.enablePolygonDrawing(isChecked)

Java

geoFenceView.enablePolygonDrawing(isChecked);

Toggle Geofence type

To draw circle Geofence

Kotlin

geoFenceView.drawCircleGeoFence()

Java

geoFenceView.drawCircleGeoFence();

To draw polygon Geofence

Kotlin

geoFenceView.drawPolygonGeofence()

Java

geoFenceView.drawPolygonGeofence();

To draw Rectangle Geofence

Kotlin

geoFenceView.drawQuadrilateralGeofence()

Java

geoFenceView.drawQuadrilateralGeofence();

Restrict number of points in a polygon

Kotlin

geofenceView.setMaxAllowedEdgesInAPolygon(12)

Java

geofenceView.setMaxAllowedEdgesInAPolygon(12);

Callback when user tries to add more than restricted points in a polygon

Kotlin

geoFenceView.setOnPolygonReachedMaxPointListener(object: OnPolygonReachedMaxPointListener {
    override fun onReachedMaxPoint() {
        //show a message to the user
    }
})

Java

geoFenceView.setOnPolygonReachedMaxPointListener(new OnPolygonReachedMaxPointListener() {
    @Override
    public void onReachedMaxPoint() {
        //show a message to the user
    }
});

Auto correct self intersecting polygon

Kotlin

geofenceView.simplifyWhenIntersectingPolygonDetected(true)

Java

geofenceView.simplifyWhenIntersectingPolygonDetected(true);

Set Normal/Rectangle Polygon

Kotlin

geoFence.polygonType = PolygonType.QUADRILATERAL
        //Normal
geoFence.polygonType =  PolygonType.NORMAL

Java

geoFence.setPolygonType(PolygonType.QUADRILATERAL);
             //Normal
geoFence.setPolygonType(PolygonType.NORMAL);

get center of plotted polygon

Kotlin

geoFence.getPolygonCentre()

Java

geofence.getPolygonCentre();

get Polygon points in clockwise/anti-clockwise directions

Kotlin

geoFenceView.convertPointsToClockWise(Orientation.CLOCKWISE)

Java

geoFenceView.convertPointsToClockWise(Orientation.CLOCKWISE);

To set Camera Padding for zoom on change geofence

Kotlin

geoFenceView.setCameraPadding(left, top, right, bottom)

Java

geoFenceView.setCameraPadding(left, top, right, bottom);

To disable zoom on change geofence

Kotlin

geoFenceView.shouldAllowAutoZoom(false)

Java

geoFenceView.shouldAllowAutoZoom(false);

For any queries and support, please contact:

Email us at apisupport@mappls.com

Support Need support? contact us!



@ Copyright 2025 CE Info Systems Ltd. All Rights Reserved.