2017-05-27 32 views
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) 

     } 


    } 

} 

回答

1

在我的應用我有每個不同圖像的兩個不同的註解: - CameraNotation和NoteAnnotation。我爲每個像這樣的子類MGLAnnotation:

// 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" 
} 
} 

注意reuseIdentifier。爲每個註釋類型設置一個新的。然後在你的viewController,您可以檢查每個這樣的:

func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? { 
    // We're not concerned with the user annotation. 
    guard annotation is CameraAnnotation || annotation is NoteAnnotation else { 
     return nil 
    } 

    // For better performance, reuse existing annotations. To use multiple different annotation views, change the reuse identifier for each. 
    if annotation is CameraAnnotation { 
     if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "cameraAnnotation") { 
      return annotationView 
     } else { 
      return DraggableAnnotationView(reuseIdentifier: "cameraAnnotation", size: CGSize(width: 39, height: 39), annotation: annotation) 
     } 
    } else if annotation is NoteAnnotation { 
     if let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "noteAnnotation") { 
      return annotationView 
     } else { 
      return DraggableAnnotationView(reuseIdentifier: "noteAnnotation", size: CGSize(width: 39, height: 39), annotation: annotation) 
     } 
    } else { 
     return DraggableAnnotationView(reuseIdentifier: "draggablePoint", size: CGSize(width: 39, height: 39), annotation: annotation) 
    } 
} 

我的分數可以拖動因此DraggableAnnotationView通話,但你走你自己的路。希望這可以幫助。

+0

好吧,謝謝你的幫助!當我通過地圖上的位置進行篩選時,我想知道是否有比創建三種不同的自定義註釋更快的方法?我已經展示瞭如何過濾我上面的註釋。任何指導都會很棒 – Cal

+0

我可能錯了,稍後我會重新檢查文檔,但我相信您正在使用的MGLPointAnnotation是一種「簡單情況」或「簡單模式」註釋,用於當您希望快速獲得某些內容時簡單標記在地圖上。對於更多涉及的內容,例如改變註釋的外觀或行爲,您需要繼承MGLAnnotation。希望我錯了,但我的直覺卻不然。當我有機會在Mapbox API文檔中檢查MGLPointAnnotation時,我會再次發佈。 – Magnas

+0

耶看起來像我錯了!此Mapbox網站上的示例允許使用MGLPointAnnotations更改視圖。 https://www.mapbox.com/ios-sdk/examples/annotation-views/他們對reuseIdentifier使用了一個有趣的局部座標,但是您會使用一些相關的字符串作爲您的用例。 – Magnas

相關問題