简体   繁体   中英

Swift - Pulling a link from init parameters to MapView annotation

I am working on a practice app to learn swift which places pins on a map and has a clickable link to each pin's corresponding website via the detail disclosure. I am able to pull the coordinates as well as the title and subtitle for each pin from an init but am struggling on pulling the "site" from each corresponding location.

import UIKit
import MapKit

class TruckAnnotation:NSObject,MKAnnotation{
    var coordinate: CLLocationCoordinate2D
    var title: String?
    var subtitle: String?
    var site: String?
    init(_ latitude:CLLocationDegrees,_ longitude:CLLocationDegrees,title:String,subtitle:String,site:String){
        self.coordinate = CLLocationCoordinate2DMake(latitude, longitude)
        self.title = title
        self.subtitle = subtitle
        self.site = site
    }
}
 
 
class TruckAnnotations: NSObject {
    var trucks:[TruckAnnotation]
    
    override init(){
        trucks = [TruckAnnotation(41.6614,-72.6076, title: "Truck 1", subtitle:"Brewery 1", site: "https://www.google.com")]
        trucks += [TruckAnnotation(41.7617,-72.5776, title: "Truck 2", subtitle:"Brewery 2", site: "https://www.google.com")]
        trucks += [TruckAnnotation(41.6580,-72.6524, title: "Truck 3", subtitle:"Brewery 3", site: "https://www.google.com")]
        trucks += [TruckAnnotation(41.5875,-72.5823, title: "Truck 4", subtitle:"Park 1", site: "https://www.google.com")]
         trucks += [TruckAnnotation(41.6747,-72.5476, title: "Truck 5", subtitle:"Brewery 4", site: "https://www.google.com")]
         trucks += [TruckAnnotation(41.6699,-72.7568 , title: "Truck 6", subtitle:"Vineyard 1", site: "https://www.google.com")]
         trucks += [TruckAnnotation(41.7421,-72.6312 , title: "Truck 7", subtitle:"Brewery 5", site: "https://www.google.com")]
         trucks += [TruckAnnotation(41.6312,-72.5846, title: "Truck 8", subtitle:"Vineyard 2", site: "https://www.google.com")]
         trucks += [TruckAnnotation(41.8202,-72.5100, title: "Truck 9", subtitle:"Park 2", site: "https://www.google.com")]
    }
}

Below is how I am using the annotations in the mapkit.

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    var annotationView = MKMarkerAnnotationView()
    guard let annotation = annotation as? TruckAnnotation else {return nil}
    let identifier = ""
    if let dequedView = mapView.dequeueReusableAnnotationView(
        withIdentifier: identifier)
        as? MKMarkerAnnotationView {
        annotationView = dequedView
    } else{
        annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
    }
    annotationView.canShowCallout = true
    annotationView.calloutOffset  = CGPoint(x: 0, y:5)
    annotationView.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIButton
    annotationView.leftCalloutAccessoryView = UIButton(type: .detailDisclosure) as UIButton
    annotationView.clusteringIdentifier = identifier
    return annotationView
}

func mapView(_ mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    
    let buttonPressed = annotationView.annotation!.title!
    let buttonPressedMid =
    
        if control == annotationView.rightCalloutAccessoryView {
        print("Right Disclosure Pressed!")
        print("\(String(describing: annotationView.annotation!.title!))")
        print("https://www.\(String(describing: buttonPressed)).com")
        print("site link")
        if let url = NSURL(string: "https://www.google.com"){
            UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)            }
    }
    if control == annotationView.leftCalloutAccessoryView {
        print("Left Disclosure Pressed!")
        if let url = NSURL(string: "https://www.yahoo.com"){
           UIApplication.shared.open(url as URL, options: [:], completionHandler: nil)            }
    }
}

Obviously Google.com/Yahoo.com are placeholders and I am trying to pull unique values from each collection.

(If you can't tell, I am very much a beginner, so please bear with me. This is a learning exercise to become more familiar with ios development).

Thank you.

Often, in calloutAccessoryControlTapped , we would cast to our custom annotation:

guard let annotation = annotationView.annotation as? TruckAnnotation else { return } 

Then we can retrieve the custom properties (of the TruckAnnotation in this case) to determine the correct course of action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM