2017-08-24 74 views
2

我很難顯示自定義註釋視圖。具體來說,我試圖設置一個名爲「pin」的圖像作爲新的地圖引腳。默認引腳總是顯示。我已經做了幾個小時的小改動,例如將「pin」改爲「pin.png」,並改變了方法的結構。這是我的。你可以看看有什麼突出的東西嗎?無法創建自定義MKAnnotationView

感謝您的幫助!

註釋類:

class Annotation: NSObject, MKAnnotation { 
    dynamic var coordinate : CLLocationCoordinate2D 
    var title: String? 
    var subtitle: String? 

    init(location coord:CLLocationCoordinate2D) { 
     self.coordinate = coord 
     super.init() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 

AnnotationView類:

class AnnotationView : MKAnnotationView { 
    override init(annotation:MKAnnotation?, reuseIdentifier: String?) { 
     super.init(annotation: annotation, 
       reuseIdentifier: reuseIdentifier) 
     let im = UIImage(named: "pin")! 
     self.frame = CGRect(x: 0, y: 0, width: im.size.width/3.0 + 5, height: im.size.height/3.0 + 5) 
     self.centerOffset = CGPoint(x: 0, y: -20) 
     self.isOpaque = false 
    } 
    required init (coder: NSCoder) { 
     fatalError("NSCoding not supported") 
    } 
    override func draw(_ rect: CGRect) { 
     let im = UIImage(named: "pin")! 
     im.draw(in: self.bounds.insetBy(dx: 5, dy: 5)) 
    } 
} 

的MapView:viewFor:方法:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    var v : MKAnnotationView! = nil 
    let ident = "pin" 
    v = mapView.dequeueReusableAnnotationView(withIdentifier: ident) 
    if v == nil { 
     v = AnnotationView(annotation: annotation, reuseIdentifier: ident) 
     v.canShowCallout = true 
    } 
    v.annotation = annotation 
    return v 
} 

其他相關方法:

@IBAction func submitDog(_ sender: Any) { 
    let newDog = Dog(name: newDogName.text!, score: 11, picture: image!, location: location!) 
    dogs.append(newDog) 
    print(dogs.last!) 
    UIView.animate(withDuration: 0.5, animations: { 

    }) { _ in 
     self.newDogView.animation = "slideUp" 
     self.newDogView.animate() 
     self.newDogView.isHidden = true 
     self.newDogName.text = "" 
     self.map.isUserInteractionEnabled = true 
    } 
    dropNewPin(locatedAt: dogs.last!.location, name: dogs.last!.name, rate: dogs.last!.score) 
} 

func dropNewPin(locatedAt: CLLocation, name: String, rate: Int) { 
    let annotation = Annotation(location: CLLocationCoordinate2D(latitude: locatedAt.coordinate.latitude, longitude: locatedAt.coordinate.longitude)) 
    annotation.title = name 
    annotation.subtitle = "\(rate)/10" 
    self.map.addAnnotation(annotation) 
} 
+0

您是否添加了'self.mapView.delgate = self'?我沒有看到那行 –

+0

檢查你是否在名稱銷的資產中的圖像,對我來說你的代碼工作,但它不是正確的方式做到這一點 –

+0

@ReinierMelian設置mapview委託工作!謝謝!但它引入了一些意想不到的行爲您的位置的默認註釋現在也被設置爲「pin」圖像。 –

回答

1

首先,你需要添加你的viewController作爲地圖

self.mapView.delegate = self 

的委託後,我建議你使用MKAnnotationView,而不是修改和自定義繪圖添加圖像,如果你需要一個定製的註解視圖,那麼你需要添加xib文件和您的自定義類作爲文件所有者並進行適當的調整

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    //to avoid make a custom Annotation view for your user location 
    if(annotation is MKUserLocation){ 
     return nil 
    } 

    let ident = "pin" 
    var v = mapView.dequeueReusableAnnotationView(withIdentifier: ident) 
    if v == nil { 
     v = MKAnnotationView(annotation: annotation, reuseIdentifier: ident) 
     v?.image = UIImage(named: "pin") 
     v?.canShowCallout = true 
    } 
    v?.annotation = annotation 
    return v 
} 
+0

要清楚,不需要定製MKAnnotationView類嗎?我應該使用自定義的MKAnnotation類嗎?我的下一步是定製標註。 –

+0

如果您的自定義annotationView設計太複雜,那麼您需要製作一個自定義註釋視圖類,但是如果您的註釋設計只是一個圖像,那麼不需要做@CodyPotter,自定義標註是另一部分是分離的問題, MKAnnotation是一個協議,實際上任何類都可以實現只有一個座標屬性 –