0
在我的應用程序中,我需要四個不同的註釋圖像來表示不同的位置類型。我瀏覽過並找到了有關此主題的一些信息,但與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)
}
}
}
好吧,謝謝你的幫助!當我通過地圖上的位置進行篩選時,我想知道是否有比創建三種不同的自定義註釋更快的方法?我已經展示瞭如何過濾我上面的註釋。任何指導都會很棒 – Cal
我可能錯了,稍後我會重新檢查文檔,但我相信您正在使用的MGLPointAnnotation是一種「簡單情況」或「簡單模式」註釋,用於當您希望快速獲得某些內容時簡單標記在地圖上。對於更多涉及的內容,例如改變註釋的外觀或行爲,您需要繼承MGLAnnotation。希望我錯了,但我的直覺卻不然。當我有機會在Mapbox API文檔中檢查MGLPointAnnotation時,我會再次發佈。 – Magnas
耶看起來像我錯了!此Mapbox網站上的示例允許使用MGLPointAnnotations更改視圖。 https://www.mapbox.com/ios-sdk/examples/annotation-views/他們對reuseIdentifier使用了一個有趣的局部座標,但是您會使用一些相關的字符串作爲您的用例。 – Magnas