繁体   English   中英

Swift MapKit-注解不会显示在MapView中

[英]Swift MapKit - Annotations dont show in the MapView

我正在尝试使用MapKit框架创建一个MapView,以查找当前用户位置并显示附近的餐馆。 但是,当我尝试显示注释时,它们不会显示。

我尝试打印(response.mapItems)来检查它是否有效,并且结果很好,因为它会打印有关在控制台中找到的一些餐厅的信息。

因此,我不知道为什么这些没有在地图上标注。

这是我编写的代码:

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)
            }

        }

    }
}

}

您的注释实现有点差,让我解释一下它应该如何。

向地图添加注释取决于两点。

  1. 符合MKAnnotation的类。
  2. 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的实现。

实现viewForAnnotation

   func mapView(_ mapView: MKMapView,viewFor annotation: MKAnnotation) -> MKAnnotationView?

在这里查看我的演示customPinAnnotationButton

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM