mapbox / mapbox-annotation-extension

Framework extensions that can be used with the Mapbox Maps SDK for iOS.
ISC License
11 stars 19 forks source link

Relationship between AnnotationController and StyleAnnotations. #56

Open TimothySealy opened 4 years ago

TimothySealy commented 4 years ago

I have a question regarding the relationship between a controller and its annotations. Does every annotation need its own controller or is it best practice to manage multiple annotation using one controller?

In case of the latter how do we group the annotations for a controller? Of course you should create a MGLSymbolStyleAnnotation for a MGLSymbolAnnotationController but do you add all symbols to one controller (even if you want to display different types of symbols)?

captainbarbosa commented 4 years ago

Hi @TimothySealy, great question!

Does every annotation need its own controller or is it best practice to manage multiple annotation using one controller?

It expected that multiple MGLStyleAnnotations should be managed by one controller. You don't need to adhere to a strict "1 annotation = 1 controller" rule.

For example, if I wanted to add two symbol annotations to the map, each with their own styling, this is valid:

let symbolAnnotationController = MGLSymbolAnnotationController(mapView: self.mapView)

let parkingLot = MGLSymbolStyleAnnotation(coordinate: CLLocationCoordinate2DMake(10, 15));
parkingLot.iconImageName = "parking-lot"

let restaurant = MGLSymbolStyleAnnotation(coordinate: CLLocationCoordinate2DMake(9, 10));
restaurant.iconImageName = "restaurant

symbolAnnotationController.addStyleAnnotations([parkingLot, restaurant])

However, there are a handful of layout properties that belong to MGLSymbolAnnotationControllers that will apply layout-related styling to all of its child annotations (such as icon image padding and maximum text angle). If you wanted to apply different layout properties for individual annotations, adding multiple MGLSymbolAnnotationControllers makes sense. So, this is valid too:

let parkinglotSymbolAnnotationController = MGLSymbolAnnotationController(mapView: self.mapView)
let restaurantSymbolAnnotationController = MGLSymbolAnnotationController(mapView: self.mapView)

parkinglotSymbolAnnotationController.textLineHeight = 8.0
let parkingLot = MGLSymbolStyleAnnotation(coordinate: CLLocationCoordinate2DMake(10, 15));
parkingLot.text = "Park here"

restaurantSymbolAnnotationController.textLineHeight = 8.0
let restaurant = MGLSymbolStyleAnnotation(coordinate: CLLocationCoordinate2DMake(9, 10));
restaurant.text = "Restaurant"

symbolAnnotationController.addStyleAnnotations([parkingLot, restaurant])

Hope this helps!

TimothySealy commented 4 years ago

Hi @captainbarbosa, thank you for the quick response.

I think I follow. I overlooked the layout properties of the controller. As I understand it you could have one controller for every layer you want on the map (much like the way you would using the Mapbox GL JS library).