Is there a way to add an ID or something, that i am able to set custom marker images?
I have multiple markers with numbers, and i need that every new marker has another image.
For example:
marker1 - marker_1_image
marker2 - marker_2_image
Atm i am only able to set 1 image (globaly) to all markers:
func mapView(mapView: MGLMapView, imageForAnnotation annotation: MGLAnnotation) -> MGLAnnotationImage? {
var annotationImage = mapView.dequeueReusableAnnotationImageWithIdentifier("place")
if annotationImage == nil {
var image = UIImage(named: "marker_1")!
image = image.imageWithAlignmentRectInsets(UIEdgeInsetsMake(0, 0, image.size.height/2, 0))
annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: "place")
}
return annotationImage
}
Any ideas? Or can i subclass MGLAnnotation and use that for all delegate methods?
Carrying on from my initial answer, the recommended way to do this is to simply create your own annotation class that includes the properties you need.
You should implement the
MGLAnnotation
protocol or subclass and add auserInfo
property. Both techniques are demonstrated in the official example, and here’s the former:Define your custom annotation class:
And use it in your view controller:
You could subclass and add a
userInfo
property (as in this answer), or you could use the existing title/subtitle properties:You’ll want to make the image loading more robust and customize the specificity of the reuse identifier string, but this should generally work.