小编Hug*_*o75的帖子

如何在 Mapkit 中以编程方式选择特定注释 - Swift

我正在 MapKit/Swift 4 中开发地图,其中有很多注释。用户可以使用选择器视图选择他想要的注释(仅 1),并且该注释将被调用(就好像他已经按下了它一样)。如果方便的话,我们可以考虑改变这个注释点的颜色。关键是要在众多注释中突出显示这个特定的注释。

经过一番搜索,我发现了这个功能

func selectAnnotation(_ annotation: MKAnnotation, animated: Bool)
Run Code Online (Sandbox Code Playgroud)

我已经实现了以下功能:

func selectPoints() {
    print("selectPoints called")
    let annotation1 = MKPointAnnotation()
    annotation1.coordinate = CLLocationCoordinate2D(latitude: 48.8596833, longitude: 2.3988939)
    annotation1.title = "Temple"
    annotation1.subtitle = "\(annotation1.coordinate.latitude), \(annotation1.coordinate.longitude)"
    mapView.addAnnotation(annotation1)

    mapView.selectAnnotation(annotation1, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

因此,如果我创建一个新注释,它就可以工作,但是我如何选择以前的注释点?但我没有任何继续前进的想法或暗示。

谢谢你的帮助。

编辑:输入注释的部分代码。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if let annotation = annotation as? Artwork {

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)

        if pinView == nil {
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: …
Run Code Online (Sandbox Code Playgroud)

mapkit mkannotation mapkitannotation swift

3
推荐指数
1
解决办法
4974
查看次数

迅捷-如何按字母顺序排列注释数组

我的项目是带有很多注释点的地图,由于有pickerView,用户可以查找特定的注释。一切正常,只是所有注释点的列表似乎随机显示。我想按字母顺序对注释进行排序。

这是我当前的工作代码:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        return self.mapView.annotations[row].title ?? "No title"

    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.mapView.selectAnnotation(self.mapView.annotations[row], animated: true)
    }
Run Code Online (Sandbox Code Playgroud)

我曾尝试实现这一点,但没有成功...

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

let sortedNames = self.mapView.annotations.sorted(by: {$0.title < $1.title})
        return sortedNames[row].title ?? "No title"

    }
Run Code Online (Sandbox Code Playgroud)

我有这个错误:

无法转换类型为'String ??'的值 到预期的参数类型'UIContentSizeCategory'

任何提示将不胜感激。

annotations mapkit mkannotation swift

2
推荐指数
1
解决办法
473
查看次数