Skip to content

Adding Annotations to Maps

This guide explains how the map annotations work, the tutorial explain the usage of the MapView Annotation/Marker objects, it has the name PTRMapViewAnnotation on iOS and PTRMapViewMarker on Android

Annotations can be added to the existing default layer with a predefined style. In case you want to have your own styles you can create layers for each style (text color, icon and overlap option) and add your annotations to these layers.

Create Annotation

iOS:

let annotation = PTRMapViewAnnotation(identifier: "annotation-id",
                                      coordinate: annotationCoordinate,
                                        mapLevel: annotationMapLevel,
                                            text: "Example Annotation Title")

The coordinate is an instance of CLLocationCoordinate2D with the latitude and longitude. Map level is an instance of PTRMapViewLevel with level index, building identifier and site identifier.

Android:

val annotation = PTRMapViewMarker(
   latitude = annotationLatitude,
   longitude = annotationLongitude,
   text = "Example Annotation Title")

Add annotation to default layer

iOS:

mapWidget.mapViewController.addAnnotation(annotation)

Android:

ptrMapFragment.addAnnotation(annotation, mapLevel)

where mapLevel is an instance of com.pointrlabs.core.management.models.Level.

Add annotation to your custom layer

iOS:

let layer = PTRMapViewAnnotationLayer(identifier: "layer-id", 
                                            icon: layerIcon,
                                       textColor: .blue,
                              shouldAllowOverlap: false)

mapWidget.mapViewController.addAnnotation(annotation, toLayer: layer)

Android:

val layer = PTRMapViewAnnotatiionLayer(color = Color.BLUE, 
                                        icon = annotationIcon)
ptrMapFragment.addAnnotation(annotation, level, layer)

Removing Annotations

To remove an annotation you can call:

mapWidget.mapViewController.removeAnnotation(annotation)
ptrMapFragment.removeAnnotation(annotation)

with the annotation object you created while adding it. Or if you want to remove all annotations, you can simply call:

mapWidget.mapViewController.removeAllAnnotations()
ptrMapFragment.removeAllAnnotations()

Final Considerations

Map must be ready

To add an annotation you need to be sure the map is ready for it. You should listen to the mapWillStartLoadingBaseLayers callback and only add annotations after it is triggered.

iOS:

Add PTRMapEventsListener to start listening map events by calling:

mapWidget.mapViewController.addListener(objectThatWillListenMapEvents)

and then listen for:

func mapWillStartLoadingBaseLayers(_ map: PTRMapViewController) {
 // Map is ready to load layers
}

Android:

widget.addListener(object: MapEventsListener{
   override fun mapDidEndLoading(mapFragment: PTRMapFragment) {
       // Map is loaded. You can add annotation
   }
})

where widget is a PTRMapWidgetFragment instance.


Last update: August 10, 2022
Back to top