Ios Swift MapKit-注释不会显示在地图视图中
我正在尝试使用MapKit framework创建一个MapView,它查找当前用户位置并显示附近的餐馆。但是,当我试图显示注释时,它们不会显示 我尝试打印(response.mapItems)来检查它是否有效,结果很好,因为它打印了控制台中找到的一些餐馆的信息 因此,我不知道为什么这些没有在地图上标注 以下是我编写的代码:Ios Swift MapKit-注释不会显示在地图视图中,ios,swift,xcode,geolocation,mapkit,Ios,Swift,Xcode,Geolocation,Mapkit,我正在尝试使用MapKit framework创建一个MapView,它查找当前用户位置并显示附近的餐馆。但是,当我试图显示注释时,它们不会显示 我尝试打印(response.mapItems)来检查它是否有效,结果很好,因为它打印了控制台中找到的一些餐馆的信息 因此,我不知道为什么这些没有在地图上标注 以下是我编写的代码: import UIKit import MapKit import CoreLocation class RestaurantViewController: UIView
import UIKit
import MapKit
import CoreLocation
class RestaurantViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.025, 0.025)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "restaurant"
request.region = map.region
let search = MKLocalSearch(request: request)
search.start { (response, error) in
guard let response = response else {
return
}
for item in response.mapItems {
print(response.mapItems) - Console shows some restaunrant outputs that were correctly fetched
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
DispatchQueue.main.async {
self.map.addAnnotation(annotation)
}
}
}
}
}
实现viewForAnnotation
func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?
在这里查看我的演示如果您的注释实现有点不成功,请允许我解释一下应该如何实现 向地图添加注释依赖于两件事
MKAnnotation
的类MKAnnotationView
的子类viewForAnimation
告诉它如何显示它们。请看我下面的示例:
class PinAnnotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
init(coordinate: CLLocationCoordinate2D) {
self.coordinate = coordinate
super.init()
}
}
class PinAnnotationView: MKAnnotationView {
@available(*, unavailable)
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
init(annotation: PinAnnotation) {
super.init(annotation: annotation, reuseIdentifier: nil)
self.frame = CGRect(x: 0, y: 0, width: 30, height: 30)
self.image = UIImage(named: "mypinimage") // or whatever.
}
}
这里我们有我上面提到的两个对象的定义
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let imageAnnotation = annotation as? ImageAnnotation {
let view = ImageAnnotationView(annotation: imageAnnotation)
return view
}
return nil
}
这里是我上面提到的
viewForAnnotation
的实现。我想问一下它到底是如何工作的,因为我不知道该怎么做。response.mapItems和用户位置之间的距离是分开的吗?
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let imageAnnotation = annotation as? ImageAnnotation {
let view = ImageAnnotationView(annotation: imageAnnotation)
return view
}
return nil
}