Hello Andrew,
thank you and Audrey so much for this tutorial. The last time I was actively involved in programming was almost 35 years ago and last year, due to Corona, I started looking into building an app. I have seen and read many tutorials and with yours I learned extremely much, thank you!
I built an app and based on the MapKit tutorial I added a āMapViewerā, integrated new icons and created my own GEOJSON file with new information from āmy surroundingsā.
Now, unfortunately, I have a small problem on which I get stuck. In my GeoJSON file I have also URLs (to the websites of the respective location) and also phone numbers to the locations included. Of course I would like to use these as well. In addition to the right callout button, I have also defined a left callout button with a āSafariā icon. When pressing this button, the corresponding homepage should be opened in Safari (maybe later also in a āWebViewerā in my app). But somehow I donāt get further to connect the function with this button. After many attempts, I have now returned to the source of the code where everything still worked without errors.
In Artwork.swift:
class Artwork: NSObject, MKAnnotation {
let title: String?
let locationName: String?
let discipline: String?
let link: URL?
let coordinate: CLLocationCoordinate2D
furtherā¦
init(
title: String?,
locationName: String?,
discipline: String?,
link: URL?,
coordinate: CLLocationCoordinate2D
) {
self.title = title
self.locationName = locationName
self.discipline = discipline
self.link = link
self.coordinate = coordinate
super.init()
}
// 3
title = properties["title"] as? String
locationName = properties["location"] as? String
discipline = properties["discipline"] as? String
link = NSURL(string: "link")! as URL
coordinate = point.coordinate
super.init()
In ArtworkViews.swift
canShowCallout = true
calloutOffset = CGPoint(x: -5, y: 5)
rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
leftCalloutAccessoryView = UIButton(type: .detailDisclosure)
furtherā¦
class ArtworkView: MKAnnotationView {
override var annotation: MKAnnotation? {
willSet {
guard let artwork = newValue as? Artwork else {
return
}
canShowCallout = true
calloutOffset = CGPoint(x: -5, y: 5)
let mapsButton = UIButton(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 48, height: 48)))
mapsButton.setBackgroundImage(#imageLiteral(resourceName: "Map"), for: .normal)
rightCalloutAccessoryView = mapsButton
let webButton = UIButton(frame: CGRect(origin: CGPoint.zero, size: CGSize(width: 48, height: 48)))
webButton.setBackgroundImage(#imageLiteral(resourceName: "Safari"), for: .normal)
leftCalloutAccessoryView = webButton
In this version the left and the right button have the same function (open Map). Unfortunately I failed miserably when trying to connect the URL to the left button and have it open in Safari.
In Germany they say: You have scissors in your head! That means, it is probably worst, if you have the scissors in the head, this internalized censorship, which puts shackles on you, even before you write.
Greetings from Germany
Michael