J'ai du mal à afficher une vue d'annotation personnalisée. Spécifiquement, j'essaye de placer une image nommée "pin" pour être la nouvelle broche de carte. La broche par défaut est toujours affichée. J'ai fait de petits changements pendant quelques heures en vain, comme changer «pin» en «pin.png» et en modifiant la structure de la méthode mapView:viewFor
. Voici ce que j'ai. Pourriez-vous s'il vous plaît jeter un oeil et voir si quelque chose se démarque?Problème lors de la création de MKAnnotationView personnalisé
Merci pour toute aide!
Classe Annotation:
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 Classe:
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: Méthode:
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
}
Autres méthodes pertinentes:
@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)
}
Avez-vous ajouté 'self.mapView.delgate = self'? Je ne vois pas cette ligne –
vérifier si vous avez une image dans les actifs avec nom pin, pour moi votre code fonctionne mais ce n'est pas la bonne façon de le faire –
@ReinierMelian paramètre le délégué mapview travaillé! Je vous remercie! Cependant, il a introduit un comportement inattendu. L'annotation par défaut pour votre position est maintenant définie comme l'image "pin". –