gpt4 book ai didi

ios - 如何在 swift 3 中按下 MKPointAnnotation 时调用函数

转载 作者:行者123 更新时间:2023-11-28 14:34:22 25 4
gpt4 key购买 nike

我一直在尝试所有方法来获取注释以快速调用函数。我不知道我是否做错了什么,但它就是行不通。我已经尝试在真实的 iPhone 上测试我的项目,但也没有任何反应。

这是我的代码:

import UIKit
import MapKit
import Foundation
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {

//Map

@IBOutlet weak var map: MKMapView!

let manager = CLLocationManager()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations[0]

let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
map.setRegion(region, animated: true)

let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "coffee"

// Set the region to an associated map view's region
request.region = map.region

let search = MKLocalSearch(request: request)
search.start { response, error in
guard let response = response else {
print("There was an error searching for: \(String(describing: request.naturalLanguageQuery)) error: \(String(describing: error))")
return
}

for item in response.mapItems
{
print("URL=\(String(describing: item.url ?? nil))")
print("Name=\(String(describing: item.name ?? nil))")
print("Phone Number\(String(describing: item.phoneNumber ?? nil))")


let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.map.addAnnotation(annotation)
}
}
self.map.showsUserLocation = true
}


override func viewDidLoad() {
super.viewDidLoad()

manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}

func callThisFunction() {
print("The function has been called.")
}

func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
callThisFunction()
}

我看过很多关于如何执行此操作的文章,但我不太了解。任何关于我的代码的提醒都会很棒,谢谢。

最佳答案

添加

map.delegate = self

到你的 viewDidLoad()

哦,Xcode 告诉我,

Instance method 'mapView(mapView:didSelectAnnotationView:)' nearly matches optional requirement 'mapView(_:didSelect:)' of protocol 'MKMapViewDelegate'

您也应该修复它,使用“修复”按钮。

关于ios - 如何在 swift 3 中按下 MKPointAnnotation 时调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51012650/

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