【问题标题】:iOS - Swift - How to add a button to MKPointAnnotaton - MapKitiOS - Swift - 如何向 MKPointAnnotatoton 添加按钮 - MapKit
【发布时间】:2016-07-28 09:29:54
【问题描述】:

目前我有一个地图视图,其中包含 5 个硬编码位置,这些位置在地图上用大头针显示。当用户单击这些图钉时,会向用户显示一个标注,其中包含该位置的详细信息。在这些标注中,我试图包含一个用户可以按下的按钮,然后将它们带到另一个视图控制器。

下面是我当前的代码,但是当我点击引脚时没有显示任何按钮,有人知道我缺少什么或我需要做什么才能实现按钮吗?

这是我的位置代码和当前用户位置。

import UIKit
import MapKit
import CoreLocation
import SceneKit

class SecondViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate
{
    @IBOutlet weak var mapView: MKMapView!

//Location manager property
let locationManager = CLLocationManager()

override func viewDidLoad()
{
    super.viewDidLoad()

    mapView.delegate = self

    //Find the current location as soon as the view is loaded
    self.locationManager.delegate = self

    //Setting the accuracy
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    //So only location services are used within the app
    self.locationManager.requestWhenInUseAuthorization()

    //Start up the location manager and find current location
    self.locationManager.startUpdatingLocation()

    //Use blue pin to show the location to the user
    self.mapView.showsUserLocation = true

    //Array of dictionaries to store the locations
    let locationData = [

        //Radio City Tower
        ["name": "Radio City Tower",
        "latitude": 53.4071551,
        "longitude": -2.980798600000071],

        //Metropolitian Cathedral
        ["name": "Metropolitian Catherdral",
         "latitude": 53.40494649999999,
         "longitude": -2.9684022999999797],

        //Walker Art Gallery
        ["name": "Walker Art Gallery",
            "latitude": 53.410068,
            "longitude": -2.979666],

        //Liver Buildings
        ["name": "Liver Buildings",
            "latitude": 53.405808,
            "longitude": -2.995859],

        //St George's Hall
        ["name": "St George's Hall",
            "latitude": 53.409277,
            "longitude": -2.979946]
    ]

    //Object for each dictionary in the locations array
    var annotations = [MKPointAnnotation]()

    //Populating the map with the location pins
    for Dictionary in locationData {
        let latitude = CLLocationDegrees(Dictionary["latitude"] as! Double)
        let longitude = CLLocationDegrees(Dictionary["longitude"] as! Double)
        let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        let name = Dictionary["name"] as! String
        let annotation = MKPointAnnotation()

        //Apply to annotation
        annotation.coordinate = coordinate
        annotation.title = "\(name)"
        annotations.append(annotation)


    }

    mapView.addAnnotations(annotations)

}

override func didReceiveMemoryWarning()
{
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Location Delegate Methods

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    //Gets the most current location
    let location = locations.last

     //gets the center of the last location
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

    //Creatng a region - Map will zoom to this
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: Double(0.004), longitudeDelta: Double(0.004)))

    //Apply the map view to the region
    self.mapView.setRegion(region, animated: true)

    //Stop updating the location
    self.locationManager.stopUpdatingLocation()

    let currentLocation = CLLocationCoordinate2DMake(location!.coordinate.latitude, location!.coordinate.longitude)

    //Adding an annotation to current user location
    var pinAnnotation = MKPointAnnotation()

    //Set pin coordinate to the users current location
    pinAnnotation.coordinate = currentLocation

    //Annotation title
    pinAnnotation.title = "Current Location"

    //Annotation subtitle
    pinAnnotation.subtitle = "You are here!"

    //Add the pin to the mapView
    mapView.addAnnotation(pinAnnotation)

}

这是标注中按钮实现的代码(不起作用)

    func MapView(mapview: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryTapped control: UIControl!) {
    if control == view.rightCalloutAccessoryView {
        print(view.annotation?.title)
        print(view.annotation?.subtitle)

        //Perform segue to navigate to viewcontroller
    }
}

func MapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    print("viewForAnnotation")
    if(annotation is MKUserLocation) {
        return nil
    }

    let reuseID = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView

    if(pinView == nil) {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
    }

    var button = UIButton(type: .DetailDisclosure) as UIButton

    pinView?.rightCalloutAccessoryView = button

    return pinView

}

我可能遗漏了一些非常简单的东西,但我不确定是什么。我对 iOS/Swift 开发和 MapKit 非常陌生,所以请多多包涵。我也明白这个问题已经被问过很多次了,但是我仍然无法解决这个问题。任何帮助表示赞赏,谢谢。

【问题讨论】:

  • 查看此相关帖子.. > stackoverflow.com/questions/28225296/…
  • 嗨,我已经使用了这篇文章并将代码集成到我的应用程序中并进行了必要的更改。谢谢你的评论,我很感激。

标签: ios swift swift2 mapkit


【解决方案1】:

您在以下标注方法中犯了一个小错误,在该方法中您明确解开了 MKAnnotationView!MKMapView!。这是我测试过的更正版本。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    print("viewForAnnotation")
    if(annotation is MKUserLocation) {
        return nil
    }
    let reuseID = "pin"
    var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
    if(pinView == nil) {
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
    }
    let button = UIButton()
    button.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
    let image = UIImage(named: "img.png")
    button.setImage(image, forState: .Normal)
    pinView?.rightCalloutAccessoryView = button
    return pinView
}

【讨论】:

  • 您好,感谢您的帮助。我已经对您在帖子中所说的进行了更改,但是仍然无法正常工作,它仍然只显示标注和信息,没有按钮。我还有什么问题吗?
  • 您好,请忽略我最后的评论,因为我现在已经解决了这个问题。非常感谢您的帮助,我花了 2 天时间试图解决这个问题。
猜你喜欢
  • 2014-08-03
  • 2017-05-20
  • 2017-01-16
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多