I am making a map application and I need to pin labels to the different sections of my map. These sections have to move and zoom in and out with the map itself. How would I go about doing this? I don't need the code necessarily I just need some general direction.
-
http://www.raywenderlich.com/90971/introduction-mapkit-swift-tutorial ... Pins are what you are looking for. – timgcarlson Jun 23 '15 at 15:25
-
I am not sure this tutorial works because this is gonna be on a custom image and not one that i take from the apple map application. I am making an indoor map so i dont really think it would work. – ANovice Jun 23 '15 at 15:29
-
[This SO question](http://stackoverflow.com/questions/10523122/add-uiimage-on-top-of-another-uiimage) might give you some ideas. Anything beyond creating a new image with multiple images will be a much larger task (ie to handle zooming and repositioning the "pinned" image). – timgcarlson Jun 23 '15 at 15:34
1 Answers
Welcome to Stack Overflow.
Assuming you are talking about using an MKMapView, you can create annotation objects and add them to the map. You can add them one at a time with the method addAnnotation
or add a whole array of annotation using addAnnotations
.
When the user scrolls to an area that contains annotations the map view will ask it's delegate for a view for that annotation using the mapView:viewForAnnotation:
method.
If you return nil the map view will use a standard annotation. If the user taps on an annotation the map will display a callout view, which you can also customize.
There is a fair amount to learn on using map views an annotations. Apple has a number of demo projects that show how it's done. I don't know if those demo projects have been released in Swift or not, but I bet if you google "Swift MKAnnotation demo" you'll find several.
EDIT:
If an annotation doesn't meet your needs then you could also look at adding an overlay view to your map with custom drawing. Do a search on "Adding Overlays to the Map" in the Xcode help system for more info on overlay views.
-
Can this be used on custom images though? I am currently displaying my map image in a UIscrollview. – ANovice Jun 23 '15 at 15:31
-
"Can this be used on custom images...?" What does that mean? Can you display custom images on the map? Yes. – Duncan C Jun 23 '15 at 15:32
-
I mean I do not want to use the Apple map images. I am making an indoor map of a building so having the apple map images would not help me very much. Would i be able to overlay these images in MKMapView? – ANovice Jun 23 '15 at 15:36
-
Apple added support for custom map contents a few OS versions ago. I haven't used it, but you can learn about it at this SO link: http://stackoverflow.com/questions/5691925/how-to-display-my-own-map-in-mkmapview Note that GPS readings tend to be quite bad indoors, especially in commercial buildings with metal frames. The phone might not even be able to get a GPS reading at all. You might need to use iBeacons and custom code to track the user's location. – Duncan C Jun 23 '15 at 16:29
-
My company developed a custom tiled map for indoors for a client, and used iBeacons to place the user on the map. It was quite a bit of work. – Duncan C Jun 23 '15 at 16:30