我一直在尝试一切以获得注释以快速调用函数。我不知道我是否做错了什么,但它就是行不通。我试过在真正的 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 告诉我,
实例方法 'mapView(mapView:didSelectAnnotationView:)' 几乎匹配协议 'MKMapViewDelegate' 的可选要求 'mapView(_:didSelect:)'
您也应该使用“修复”按钮修复它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句