Swift中的MapKit,第2部分 [英] MapKit in Swift, Part 2

查看:137
本文介绍了Swift中的MapKit,第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swift中的Map Kit。我尝试在地图上显示区域,一个引脚(MKPinAnnotationView)和当前位置。一切都很好。我尝试添加Disclosure Button并拦截它。添加了Disclosure Button,但拦截攻击不起作用。

I'm trying work with Map Kit in Swift. I try to display the area on the map, one pin (MKPinAnnotationView) and the current position. All display fine. I try to add Disclosure Button and intercept tapping on it. Disclosure Button added, but does not work intercept tapping.

函数 pinPressed 方法 calloutAccessoryControlTapped 无法工作....

Function pinPressed with method calloutAccessoryControlTapped not work....

这是一个示例代码:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mainMapView: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.startUpdatingLocation()

    var objectLatitude = 53.204526
    var objectLongitude = 50.111751

    var currentLatitude = 53.203715
    var currentLongitude =  50.160374

    var latDelta = 0.05
    var longDelta = 0.05

    var currentLocationSpan: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var currentLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude)
    var currentRegion: MKCoordinateRegion = MKCoordinateRegionMake(currentLocation, currentLocationSpan)
    self.mainMapView.setRegion(currentRegion, animated: true)

    var objectLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(objectLatitude, objectLongitude)
    var objectAnnotation = MKPointAnnotation()
    objectAnnotation.coordinate = objectLocation
    objectAnnotation.title = "St. George's Church"
    objectAnnotation.subtitle = "Church of the Great Martyr St. George"
    self.mainMapView.addAnnotation(objectAnnotation)
}

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            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
            pinView!.pinColor = .Purple
            pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as UIButton
        }
        else {
            pinView!.annotation = annotation
        }
        return pinView
}

func pinPressed(mapView: MKMapView!, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    if control == annotationView.rightCalloutAccessoryView {
        println("Disclosure Pressed!")
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
}


推荐答案

calloutAccessoryControlTapped 委托方法必须命名为 mapView(annotationView:calloutAccessoryControlTapped :)

The calloutAccessoryControlTapped delegate method must be named mapView(annotationView:calloutAccessoryControlTapped:).

您不能使用自己的名字,如 pinPressed(...)

You can't use your own name like pinPressed(...).

这适用于任何委托方法,并由协议规定。

This applies to any delegate method and is dictated by the protocol.

所以它应该be:

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

    if control == annotationView.rightCalloutAccessoryView {
        println("Disclosure Pressed!")
    }
}

这篇关于Swift中的MapKit,第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆