MapKit Tutorial: Getting Started

Hi Audrey,

So far I edited the code and just added two constants image and descriptions. I want to use them in my custom table view cell.

class Artwork: NSObject, MKAnnotation {
let title: String?
let locationName: String
let discipline: String
let descriptions: String
let coordinate: CLLocationCoordinate2D
let image: String

init(title: String, locationName: String, discipline: String, descriptions: String, coordinate: CLLocationCoordinate2D, image: String) {
self.title = title
self.locationName = locationName
self.discipline = discipline
self.descriptions = descriptions
self.coordinate = coordinate
self.image = image

super.init()

}

class func fromJSON(_ json: [JSONValue]) ā†’ Artwork? {
// 1
var title: String
if let titleOrNil = json[16].string {
title = titleOrNil
} else {
title = ā€œā€
}
let locationName = json[12].string
let discipline = json[15].string
let descriptions = json[11].string
let image = json[13].string
let latitude = (json[18].string! as NSString).doubleValue
let longitude = (json[19].string! as NSString).doubleValue
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

// 3
return Artwork(title: title, locationName: locationName!, discipline: discipline!, descriptions: descriptions!, coordinate: coordinate, image: image!)

}

Should the constant image be set as an UIImage instead of a string?

Also I tried googling for the AWS to change my JSON fileā€™s urls, but I not sure where they are located.

Thanks for your help so far!

you need to use URLSession to download the image, after youā€™ve fixed the url

your image property is actually imageUrl

this tutorial does something similar to what you want to do:
https://www.raywenderlich.com/76341/use-nsoperation-nsoperationqueue-swift

to fix the urls, you need to get the unique part of the JSON fileā€™s url, for example 001/193901-5 and wrap it in the correct url, i.eā€¦ put

https://s3.amazonaws.com/pastperfectonline/images/museum_220/

before 001/193901-5, and .jpg after, with no spaces.

Thanks a bunch! This is really helpful!

Is it possible to change the name of a sea in mapkit. Above UAE the sea name they use is Arabian Sea, but in ios mapkit it is persian gulf,which my client is not accepting.

You could switch to Google Maps? although they have (Arabian Gulf) in parentheses, under Persian Gulf, only if you zoom in. Possibly they show only Arabian Gulf if accessed from an Arab country, like Google Earth.

We have a tutorial: https://www.raywenderlich.com/109888/google-maps-ios-sdk-tutorial

Xcodeā€™s Swift 3 converter will hopefully get you most of the way.

Or you could try MapKit overlays:
https://www.raywenderlich.com/87008/overlay-views-mapkit-swift-tutorial

and just write over the place name?

1 Like

Hey there! I have a basic question about MapKit. Can I build an app via MapKit that will give me userā€™s driving directions natively without having to switch to the Maps app?

Thanks!

Our MapKit video course part 9 is about Directions.

https://videos.raywenderlich.com/courses/mapkit-and-core-location/lessons/9

It uses MKDirectionsRequest, which takes MKMapItems as source and destination values

https://developer.apple.com/reference/mapkit/mkdirectionsrequest

MapKit overlays worked fine( mapkit overlay).Thanks for your reply

Can I use map kit to not only display turn by turn directions with the route, but also have Siri talk the user through it similar to the maps app?

I havenā€™t used the Siri Maps feature. From whatā€™s written online, it seems to originate from Siri, that is, you ask Siri to ā€œtake me homeā€ or to guide you to a cafe or some other place that Siri can ask Maps to look for, then it brings up some map route options, you pick one, and Siri talks you there.

I think SiriKit is still limited in what you can use it for, but some of the ā€œIntentsā€ involve Maps, so one of them might fit your app.

https://developer.apple.com/library/prerelease/content/documentation/Intents/Conceptual/SiriIntegrationGuide/index.html

Hi Audrey :slight_smile:

Great tutorial!
But Iā€™m all new to swift programming, and kinda lost. Iā€™ve followed every step, and adjusted changes which you have commented on here. I have no errors in my project, but the pins are not showing. I have included the 2 files in the project, and i have called loadInitialData.
Is there any dummy mistake you could think of, i could have made?

hi Lise: the most up to date code is in the Swift 3 link ā€“ download that and compare it with your code. Look especially at anything that looks like a delegate: make sure youā€™ve added the right protocols (they might be different from the original tutorial) and have set the delegate correctly. Basically, search for delegate in my final Swift 3 code and see if yours matches.

Thanks, but the thing is, im still using swift 2, so it should pretty much be the same as in the tutorial, shouldnā€™t it :(?

I couldnā€™t say for sure ā€¦ check that youā€™ve got this line in ViewControllerā€™s viewDidLoad:

mapView.delegate = self

Can you get the single ā€œKing Kalakauaā€ pin to show? in the early part of the tutorial, before you try to show all the pins?

No i know it is an impossible question without code, but since i do not have any errors i donā€™t know where it fails - and i guess youā€™re not interested in my entire project ;b
I have mapView.delegate = self in my viewDidLoad
And yes, the single pin was working fine :confused:

There could be a problem with JSON.swift.

Try this, to see whether your Artworks are being created correctly: add a print statement to loadInitialData

// 5
  artwork = Artwork.fromJSON(artworkJSON) {
    print(artwork.title)
    artworks.append(artwork)
  }

Iā€™m currently working on an app which is almost the same to what this tutorial is about.

My objective is to create a callout accessory that opens another VC. However, I canā€™t seem to get the callout accessory to show.

I tried using a snippet of your code and use it with mine but itā€™s not working.

Could you have a look at my classes and see where Iā€™m going wrong??

Thank you!!

Second class:

Third class:

Hmm, yearh youā€™re right, nothing comes out :confused: