使用Mapbox更改地图上的注释图像

时间:2017-05-27 12:15:44

标签: ios swift firebase annotations mapbox

在我的应用程序中,我需要有四个不同的注释图像,表示不同的位置类型。我已查看并找到了有关此主题的一些信息,但与Mapbox无关。

目前,用户可以过滤不同类型的位置。这是因为在我的数据库中,注释按Firebase中的类型进行区分。类型:1 =滑板公园,类型:2 =街头滑冰等。

通过阅读有关堆栈溢出的信息,我相信我需要创建一个自定义注释,我已经完成了。

class SkateAnnotation: MGLPointAnnotation {

var canEdit = false
var id: String!
var type: SkateType!

}

我已将该类型应用于我的注释。

  func addAnnotation(park: Skatepark) {

    let point = SkateAnnotation()

    point.coordinate = park.coordinate

    point.title = park.name

    point.id = park.id

    point.subtitle = park.subtitle

    point.canEdit = park.editable

    point.type = park.type

    mapView.addAnnotation(point)

    mapView.selectAnnotation(point, animated: true)

}

我唯一感到困惑的部分是将这些类型应用于不同的图像。目前我的代码看起来像这样,它只将一个图像应用于所有注释。

    func mapView(_ mapView: MGLMapView, imageFor annotation: MGLAnnotation) -> MGLAnnotationImage? {




 //   return nil

    var annotationImage = mapView.dequeueReusableAnnotationImage(withIdentifier: "SkateAnnotation1")

    if annotationImage == nil {

        var image = UIImage(named: "SkateAnnotation1")!




        image = image.withAlignmentRectInsets(UIEdgeInsets(top: 0, left: 0, bottom: image.size.height / 2, right: 0))

        annotationImage = MGLAnnotationImage(image: image, reuseIdentifier: "SkateAnnotation1")



    }

    return annotationImage

}

}

是否有人能够根据类型将图像设置为注释?

我的过滤器注释代码:

  func sideBarDidSelectButtonAtIndex(_ index: Int) {

   mapView.removeAnnotations(mapView.annotations!)

    for park in skateparks {

        if index == 0 {
            addAnnotation(park: park)


        }

        if index == 1 && park.type == .park {
            addAnnotation(park: park)


        }

        if index == 2 && park.type == .street {
            addAnnotation(park: park)

        }

        //Change this to feature the users own personal spots they saved to firebase

        if index == 3 && park.type == .own {
            addAnnotation(park: park)

        }


    }

}

1 个答案:

答案 0 :(得分:1)

在我的应用程序中,我有两个不同的注释,每个注释都有不同的图像: - CameraNotation和NoteAnnotation。我像这样为每个子类化MGLAnnotation:

await launcher.LaunchAsync();

注意reuseIdentifier。为每个注释类型设置一个新的。然后在viewController中,您可以像这样检查每个:

// MGLAnnotation protocol reimplementation
class NoteAnnotation: NSObject, MGLAnnotation {

// As a reimplementation of the MGLAnnotation protocol, we have to add mutable coordinate and (sub)title properties ourselves.
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?

// Custom properties that we will use to customize the annotation.
var image: UIImage?
var reuseIdentifier: String?
var uuid: String?

init(coordinate: CLLocationCoordinate2D, title: String?, subtitle: String?) {
    self.coordinate = coordinate
    self.title = title
    self.subtitle = subtitle

    self.reuseIdentifier = "noteAnnotation"
}
}

我的点是可拖动的,因此DraggableAnnotationView调用但你按照自己的方式进行。希望这会有所帮助。