gpt4 book ai didi

swift - Swift 中的 MapKit,第 2 部分

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:34 25 4
gpt4 key购买 nike

我正在尝试在 Swift 中使用 Map Kit。我尝试在 map 上显示区域、一个图钉 (MKPinAnnotationView) 和当前位置。都显示正常。我尝试添加披露按钮并拦截点击它。添加了披露按钮,但无法拦截窃听。

函数 pinPressed 和方法 calloutAccessoryControlTapped 不工作....

这是一个示例代码:

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 委托(delegate)方法,因为我今天 (09/06/2015) 尝试了它,但没有用。此输入字段已更改:annotationView view: MKAnnotationView!

//Click on left or right button
func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!)
{
let anotation = view.annotation as! MyAnnotation

if (control == view.rightCalloutAccessoryView)
{
println("Button right pressed!")
}
else if (control == view.leftCalloutAccessoryView)
{
println("Button left pressed!")
}
}

关于swift - Swift 中的 MapKit,第 2 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25202613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com