MapKit Tutorial: Getting Started

look at my Apr 15 post, about JSON.swift

it’s usually a delegate problem:

I added the code right at the top:

newFeatureMapView.delegate = self in the viewDidLoad.

Currently, I am getting the space where the callout accessory would be, but there is no [i] on the right.

I can’t see anything in your code that would prevent the callout button to appear.

Try the usual clean, super-clean, delete derived data, restart Xcode, restart Mac sequence?

Hmm, april 15, are you refering to this post?

ah, it’s the definition of NSDictionary that’s changed! I made this adjustment last month:
replace the last line of this:

case let value as NSDictionary:
var jsonObject: [String:JSONValue] = [:]
for (k: AnyObject, v: AnyObject) in value {
with this:

for (k, v) in value {

that is, remove the types of k and v


Because, unfortunately, i aldready did that :frowning:

Okay. I tried to update to swift 3 and follow your example there.
Now i get this

fatal error: unexpectedly found nil while unwrapping an Optional value

Can’t see i’ve done anything different from you :frowning:

make sure you’ve connected the map view to the property in the view controller

Wow, that was truly a dummy mistake, thank you very much :slight_smile:

easy to forget, I did it myself recently :wink:

Hello thank you for this tutorial. Can you help me because I would like to see my map with an annotation but after pressing a button. Let me explain. I have a view with the photo of a place, a label with some explanation and a button. I would like by clicking on the button to reach the map and that the place of the view is to mark on the view of the map according to the latitude and longitude. Thank you in advance if you can help me or point me

set up the button to trigger a segue to the view controller that shows the map; pass coordinates to this view controller in prepareForSegue (use the new Swift 3 signature if appropriate)

your MapViewController should be similar to this tutorial’s ViewController, just the beginning part, where it displays the single pin for King Kalakaua’s statue; don’t worry about opening the Maps app.

Thank you for coming back. But in fact I do not understand your answer
 And I do not see how to do it using latitude and longitude dans le prepareForSegue

do you know how to create segues? if not, try this tutorial:

https://www.raywenderlich.com/113394/storyboards-tutorial-in-ios-9-part-2

suppose your existing view controller, for the view with photo and button, is PhotoViewController. In the storyboard, you drag in a new view controller, then control-drag from the button to this new view, selecting Show or maybe modal.

this new view controller’s code will look a lot like the ViewController in this MapKit tutorial. It will have some properties, including latitude and longitude, which you set in PhotoViewController’s prepareForSegue.

Hi again Audrey :slight_smile:
I hope you can might help me again.
I’m building an app where i am using a lot of your tutorial - I am just using another api to get information about the annotations.
I would like the map to open up at my current location as center. This seems to be a problem.
When i open the app, the blue dot is showing, but the map is not centered around it, and i can not figure out why. This is a part of my code

I have been forced to implement a button which centers the map:

@IBAction func centerLocation(_ sender: Any) {
let userLocation = mapView.userLocation
let region = MKCoordinateRegionMakeWithDistance(
userLocation.location!.coordinate, 500, 500)

    mapView.setRegion(region, animated: true)
}

But it would be nice to have the map centered from start - it seems like i am doing something in the wrong order, but i can not figure out what (the updated longitude and latitude is not working properly either :frowning: )

Hope you can help me - again :smiley:

hi Lise: try setting breakpoints and inspecting your values to see what’s happening. Apple’s documentation for debugging in Xcode is pretty good:

https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/debugging_with_xcode/chapters/quickstart.html#//apple_ref/doc/uid/TP40015022-CH7-SW3

I kinda do know what is going on already - the focus is set to be around longitude and latitude 0.0 from the start, I just don’t know how to change it, and what i am doing wrong :frowning:

Hi Audrey,

Thank you for the tutorial, it’s helped me greatly.

What I’m trying to figure out is when a user clicks on the “right calloutaccessory”, rather than opening the maps app it would open a SecondVC and the title and subtitle of that map pin would be passed onto the SecondVC.

How would I go about doing that? I’m struggling to get my head around it. I know how to do it with textfields and labels but since we are dealing with JSON files, I am clueless.

So far, I’ve been able to create the SecondVC when the calloutAccessory is tapped.

Thank you!!

Look at a prepare(for:sender:) method — it creates another view controller, then sets that view controller’s properties.

You don’t have a segue, so you would have to call present (used to be called presentViewController) to show your second view controller

I don’t understand what you mean by that.

calloutAccessoryControlTapped already has the info you need to pass to your second view controller:

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, 
    calloutAccessoryControlTapped control: UIControl!) {
  let location = view.annotation as! Artwork
 // 1. create second VC 
 // 2. set its `title` and `subtitle` properties to location.title and location.subtitle
 // 3. present second VC
}

you’ve already done 1. and 3. so you just need to do 2.