0

I am displaying custom roads on the map and to display details about road, I need to display annotation with custom images (a shield).

But now I want to display shields with road numbers. As there are so many different roads, I cannot add all these shield with numbers in app. I want to keep only one shield image in app and write number on the shield dynamically while setting as annotation image.

There is a IconGenerator library in Google Maps API which can do this task. So I want to do the same with MKMapKit in my iOS Swift application.

Thanks in advance.

Hardik
  • 139
  • 1
  • 10
  • You can also add your own `MKAnnotation` class with a custom `drawRect`. E.g. http://stackoverflow.com/a/30415714/1271826. – Rob Oct 01 '16 at 22:51

2 Answers2

1

If you know that your image is always going to be certain dimensions (say, a square) then you know where the white space for the number will be. You can create a DynamicShield class which has the common image and a UILabel which positions the text in accordance with the dimensions of the common shield image.

0

I have created imageview and added image and label in imageview and set imageview as annotation image.

let annotationview = MKAnnotationView.init(annotation: annotationPoint, reuseIdentifier: "xyz")
annotationview.canShowCallout = true

var imageview = UIImageView(frame: CGRect(x: -15, y: -15, width: 30, height: 30))
var label = UILabel()
label.frame = CGRect(x: 0, y: 1, width: 30, height: 30)
label.textAlignment = .Center
label.font = UIFont.boldSystemFontOfSize(12)
label.textColor = UIColor.GrayColor()
label.text = “my-text”
imageview.addSubview(label)

imageview.image = UIImage(named: “image-name”)
annotationview.addSubview(imageview)

return annotationview
Hardik
  • 139
  • 1
  • 10