Zack Erickson
Zack Erickson

Reputation: 21

Adding custom location points to MKMapView

I am working on an app that uses MKMapView / Apple maps as a base, and I want to add custom points to the map to be able to have a "user" able to move from one point to the other if specific criteria is met. How would I be able to add a custom map location icon, and a user icon? I have looked into overlays and other things, and I have honestly gotten lost in all of it.

The project is kept within a specific area on the map, I would like to add 3 different types of objects to specific longitude and latitude coordinates and have the user move from one to the other. Any suggestion on how to get the points on the map?

Things that I have tried:

  1. placemark
  2. mapitem
  3. mkmappointforcoordinate
  4. mkmappoint

Upvotes: 2

Views: 1338

Answers (1)

TwoStraws
TwoStraws

Reputation: 13127

There are a few things to make this work. First, you need a custom class that builds on MKAnnotation, providing a title and a co-ordinate. Here's one to store capital cities, for example:

class Capital: NSObject, MKAnnotation {
    var title: String?
    var coordinate: CLLocationCoordinate2D
    var info: String

    init(title: String, coordinate: CLLocationCoordinate2D, info: String) {
        self.title = title
        self.coordinate = coordinate
        self.info = info
    }
}

You then create your annotation objects with your data, i.e. the positions you want to appear on the map. Here is how you might fill in that Capital annotation:

let london = Capital(title: "London", coordinate: CLLocationCoordinate2D(latitude: 51.507222, longitude: -0.1275), info: "Home to the 2012 Summer Olympics.")
let oslo = Capital(title: "Oslo", coordinate: CLLocationCoordinate2D(latitude: 59.95, longitude: 10.75), info: "Founded over a thousand years ago.")

Next, add your annotations to your map view either individually:

mapView.addAnnotation(london)
mapView.addAnnotation(oslo)

Or as an array of lots of items:

mapView.addAnnotations([london, oslo])

Finally, make your view controller the delegate of your map view, and implement viewForAnnotation so that you can show some information when the user taps on your city pin. This is a basic example:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    let identifier = "Capital"

    if annotation.isKindOfClass(Capital.self) {
        var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

        if annotationView == nil {
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            annotationView!.canShowCallout = true
        } else {
            annotationView!.annotation = annotation
        }

        return annotationView
    }

    return nil
}

Now, each annotation is unique as far as iOS is concerned, so if you wanted to make one a picture of London and another a picture of Oslo, or if you just wanted different pin colours, that's fine – it's really down to you. Your "user icon" can be whatever you want, just set the image property of your annotation view.

I hope this points you in the right direction. Good luck!

Upvotes: 2

Related Questions