Mapkit标注到单个页面,具体取决于所选的注释

时间:2017-11-22 21:27:36

标签: ios swift annotations mapkit callout

我的地图上有20个针脚,每个针脚点击时会显示一个带有场地名称的注释,并在右侧有一个标注按钮。我已经使用segue将此按钮成功链接到单个页面。事情是所有位置都链接到同一页面。我想要20个单独的页面,每个页面链接到一个特定的引脚。我在网上发现的几乎所有例子都想生成一个传入信息的单页。这是我的代码。

import UIKit
import MapKit
import CoreLocation

class MapContollerViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    // NEED TO SET THIS INT SOMEWHERE DEPENDING ON WHICH LOCATION IS SELECTED
    var venueNo: Int = 2

    // MARK: - CLLocationManager
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        checkLocationAuthorizationStatus()
    }

    let locationManager = CLLocationManager()
    func checkLocationAuthorizationStatus() {
        if CLLocationManager.authorizationStatus() == .authorizedAlways {
            mapView.showsUserLocation = true
        } else {
            locationManager.requestAlwaysAuthorization()
        }
    }

    let regionRadius: CLLocationDistance = 800
    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                                                                  regionRadius, regionRadius)
        mapView.setRegion(coordinateRegion, animated: true)
    }

    override func viewDidLoad() {
        let classInstance = MapContollerViewController() //swift allocates memory and creates an instance
        classInstance.venueNo = 2

        super.viewDidLoad()

        mapView.delegate = self

        let initialLocation = CLLocation(latitude: 56.457539, longitude: -2.9821680000000015)

        //UNION
        let artwork = Artwork(title: "The Union",
                              locationName: "Airlie Place",
                              discipline: "Venue",
                              coordinate: CLLocationCoordinate2D(latitude: 56.457539, longitude: -2.9821680000000015),
                                venueNo:1)

        //changing value here but in wrong place
        venueNo = 1

        mapView.addAnnotation(artwork)

        // TINSMITH

        let artwork2 = Artwork(title: "The Tin Smith",
                              locationName: "11-13 Old Hawkhill",
                              discipline: "Venue",
                              coordinate: CLLocationCoordinate2D(latitude: 56.4594785, longitude: -2.980198400000063),
                              venueNo:2)



        mapView.addAnnotation(artwork2)

        // Grafters

        let artwork3 = Artwork(title: "Grafters",
                               locationName: "Union Street",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.4588498, longitude: -2.9709077999999636),
                               venueNo:3)

        mapView.addAnnotation(artwork3)

        // The Bird & Bear

        let artwork4 = Artwork(title: "The Bird & Bear",
                               locationName: "2 Whitehall Cres",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.4592973, longitude: -2.968744700000002),
                               venueNo:4)

        mapView.addAnnotation(artwork4)

        // The Salty Dog

        let artwork5 = Artwork(title: "The Salty Dog",
                               locationName: "9 Crichton St",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.459916, longitude: -2.9701755999999477),
                               venueNo:5)

        mapView.addAnnotation(artwork5)

        // Jam Jar

        let artwork6 = Artwork(title: "Jam Jar",
                               locationName: "2b Whitehall Cres",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.45916279999999, longitude: -2.9688820999999734),
                               venueNo:6)

        mapView.addAnnotation(artwork6)

        // Church

        let artwork7 = Artwork(title: "Church",
                               locationName: "15 Ward Rd",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.4617273, longitude: -2.9758415000000014),
                               venueNo:7)

        mapView.addAnnotation(artwork7)

        // No'1s

        let artwork8 = Artwork(title: "No'1s",
                               locationName: "1 Ward Rd",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.4618699, longitude: -2.9745715000000246),
                               venueNo:8)

        mapView.addAnnotation(artwork8)

        // Medina

        let artwork9 = Artwork(title: "Medina",
                               locationName: "113 Nethergate",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.4576889, longitude: -2.9751609),
                               venueNo:9)

        mapView.addAnnotation(artwork9)

        // Westport

        let artwork10 = Artwork(title: "Westport Bar",
                               locationName: "N Lindsay St",
                               discipline: "Venue",
                               coordinate: CLLocationCoordinate2D(latitude: 56.4606814, longitude: -2.975895000000037),
                               venueNo:10)

        mapView.addAnnotation(artwork10)

        // D'Arcy thompson

        let artwork11 = Artwork(title: "D'Arcy Thompson",
                                locationName: "Old Hawkhill",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.4594204, longitude: -2.9805648999999903),
                                venueNo:11)

        mapView.addAnnotation(artwork11)

        // Tonic

        let artwork12 = Artwork(title: "Tonic",
                                locationName: "141 Nethergate",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.4572926, longitude: -2.9762908000000152),
                                venueNo:12)

        mapView.addAnnotation(artwork12)

        // The Beer Kitchen

        let artwork13 = Artwork(title: "The Beer Kitchen",
                                locationName: "10 S Tay St",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.457705, longitude: -2.975928000000067),
                                venueNo:13)

        mapView.addAnnotation(artwork13)

        // Braes

        let artwork14 = Artwork(title: "Braes",
                                locationName: "Perth Rd",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.45660329999999, longitude: -2.9789081000000124),
                                venueNo:14)

        mapView.addAnnotation(artwork14)

        // The Nether Inn

        let artwork15 = Artwork(title: "Nether Inn",
                                locationName: "134 Nethergate",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.4580593, longitude: -2.974011799999971),
                                venueNo:15)

        mapView.addAnnotation(artwork15)

        // The Underground

        let artwork16 = Artwork(title: "Underground",
                                locationName: "25 S Tay St",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.4583296, longitude: -2.975921200000016),
                                venueNo:16)

        mapView.addAnnotation(artwork16)

        // George Orwell

        let artwork17 = Artwork(title: "George Orwell",
                                locationName: "168 Perth Rd",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.4561959, longitude: -2.986077499999965),
                                venueNo:17)

        mapView.addAnnotation(artwork17)

        // Balcony Bar

        let artwork18 = Artwork(title: "Balcony Bar",
                                locationName: "5 Ward Rd",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.4618648, longitude: -2.9748177999999825),
                                venueNo:18)

        mapView.addAnnotation(artwork18)

        // McDaniels

        let artwork19 = Artwork(title: "McDaniels",
                                locationName: "34-36 Whitehall Cres",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.45888000000001, longitude: -2.969510000000014),
                                venueNo:19)

        mapView.addAnnotation(artwork19)

        // TheWest House

        let artwork20 = Artwork(title: "The West House",
                                locationName: "2 West Port",
                                discipline: "Venue",
                                coordinate: CLLocationCoordinate2D(latitude: 56.45932519999999, longitude: -2.977865299999962),
                                venueNo:20)

        mapView.addAnnotation(artwork20)

        centerMapOnLocation(location: initialLocation)
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}

extension MapContollerViewController: MKMapViewDelegate {

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
                 calloutAccessoryControlTapped control: UIControl) {
        //* load page on click
        print("button tapped",venueNo)

        //working on this
        if venueNo == 1{
            performSegue(withIdentifier: "Home", sender: self)
        }
        if venueNo == 2{
           performSegue(withIdentifier: "TinSmith", sender: self)
        }
    }
    // 1
    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        // 2
        guard let annotation = annotation as? Artwork else { return nil }
        // 3
        let identifier = "marker"
        var view: MKMarkerAnnotationView
        // 4
        if let dequeuedView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)
            as? MKMarkerAnnotationView {
            dequeuedView.annotation = annotation
            view = dequeuedView
        } else {
            // 5
            view = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: identifier)
            view.canShowCallout = true
            view.calloutOffset = CGPoint(x: -5, y: 5)
            view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        }
        return view   
    }
}

和处理注释的脚本

import Foundation
import MapKit
import Contacts

class Artwork: NSObject, MKAnnotation {
    let title: String?
    let locationName: String
    let discipline: String
    let coordinate: CLLocationCoordinate2D
    let venueNo: Int

    init(title: String, locationName: String, discipline: String, coordinate: CLLocationCoordinate2D, venueNo : Int) {
        self.title = title
        self.locationName = locationName
        self.discipline = discipline
        self.coordinate = coordinate
        self.venueNo = (venueNo)

        super.init()
    }

    var subtitle: String? {
        return locationName 
    }
}

我对Swift很新,如果有人可以提供帮助,我会很高兴。我一直试图弄清楚它5天,所以我想我会帮忙。

1 个答案:

答案 0 :(得分:0)

您可以根据Artwork的venueNo属性来区分注释:

func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView,
             calloutAccessoryControlTapped control: UIControl) {

    guard let artwork = view.annotation as? Artwork else { return }

    let annotaionVenueNo = artwork.venueNo

    print("veneueNo = %d", annotaionVenueNo)

    // do additional process depend on annotaionVenueNo
}