【问题标题】:Swift MapKit - Annotations dont show in the MapViewSwift MapKit - MapView 中不显示注释
【发布时间】:2018-01-12 14:38:02
【问题描述】:

我正在尝试使用 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)
            }

        }

    }
}

}

【问题讨论】:

  • response.mapItems和用户位置的距离是分开的吗?

标签: ios swift xcode geolocation mapkit


【解决方案1】:

你的注解实现有点不对劲,请允许我解释一下它应该是怎样的。

向地图添加注释依赖于两件事。

  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

【讨论】:

    【解决方案2】:

    实现 viewForAnnotation

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

    在这里查看我的演示 customPinAnnotationButton

    【讨论】:

    • 我想问一下它到底是如何工作的,因为我不知道该怎么做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多