ios - 根据所选注释对各个页面进行 Mapkit 标注

标签 ios swift annotations mapkit callout

我的 map 上有 20 个图钉,点击后每个图钉都会显示带有 field 名称的注释,并且右侧有一个标注按钮。我已使用 segue 成功将此按钮链接到单个页面。问题是所有位置都链接到同一页面。我想要 20 个单独的页面,每个页面都链接到一个特定的 pin。我在网上找到的几乎所有示例都希望生成一个包含传入信息的页面。这是我的代码。

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 天了,所以我想寻求帮助。

最佳答案

您可以根据艺术品的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
}

关于ios - 根据所选注释对各个页面进行 Mapkit 标注,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47444115/

相关文章:

java - java记录上的构造函数注释

java - 我应该如何用 Java 编写自己的自定义注释

iphone - 用于 UIView 的 iOS initWithNibName

ios - 如何以编程方式快速获取保管箱中文件的完整路径

ios - 如何控制UICollectionViewCell调整大小动画?

ios - 在 SwiftUI 中为屏幕上和屏幕外的文本添加动画效果

java - 使用 AspectJ 模拟接口(interface)和方法的注解继承

ios - 自定义 UIDynamicBehavior : how do I know the total number of animation steps/ticks?

ios - 将角点扭曲为具有给定宽高比的矩形 2D 图像的四边形

ios - 以编程方式在 UIView 上设置针对 safeAreaLayoutGuide 的约束