2015-03-18 13 views
7

Próbuję zmienić obraz pin na MKMapView w Swift, ale niestety to nie działa. Wszelka Pomysł, co robię źle? Widziałem tutaj kilka przykładów, ale nie działało.Zmiana obrazu szpilki na MKMapView w Swift

import UIKit 
import MapKit 

class AlarmMapViewController: UIViewController { 
    @IBOutlet weak var map: MKMapView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     showAlarms() 
     map.showsUserLocation = true 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    func showAlarms(){ 

     map.region.center.latitude = 49 
     map.region.center.longitude = 12 
     map.region.span.latitudeDelta = 1 
     map.region.span.longitudeDelta = 1 

     for alarm in Alarms.sharedInstance.alarms { 

      let location = CLLocationCoordinate2D(
       latitude: Double(alarm.latitude), 
       longitude: Double(alarm.longtitude) 
      ) 

      let annotation = MKPointAnnotation() 
      annotation.setCoordinate(location) 
      annotation.title = alarm.name 
      annotation.subtitle = alarm.description 
      mapView(map, viewForAnnotation: annotation).annotation = annotation 
      map.addAnnotation(annotation) 
     } 
    } 

    @IBAction func zoomIn(sender: AnyObject) { 
    } 

    @IBAction func changeMapType(sender: AnyObject) { 
    } 

    func mapView(mapView: MKMapView!, 
     viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

     if annotation is MKUserLocation { 
      //return nil so map view draws "blue dot" for standard user location 
      return nil 
     } 

     let reuseId = "pin" 
     var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView 
     if pinView == nil { 
      pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      pinView!.canShowCallout = true 
      pinView!.animatesDrop = true 
      pinView!.image = UIImage(named:"GreenDot")! 

     } 
     else { 
      pinView!.annotation = annotation 
     } 

     return pinView 
    } 
} 

Zdjęcie GreenDot jest dostępne i używane w innych miejscach.

Odpowiedz

17

Nie zapomnij ustawić:

map.delegate = self 

i upewnić się UIViewController implementuje protokół MKMapViewDelegate.
Jeśli zapomnisz to zrobić, Twoja implementacja mapView:viewForAnnotation: nie zostanie wywołana dla twojej mapy.

Poza tym wygląda na to, że pinView!.animatesDrop = true zrywa niestandardowe obrazy. Musisz ustawić go na false lub użyć MKAnnotationView (który nie ma właściwości animatesDrop).

Jeśli chcesz zaimplementować niestandardową animację upuszczania, zobacz this related question.

+0

OK Zrobiłem to, teraz mogę zmienić pinColor za pomocą pinView! .pinColor = .Green, ale kiedy próbuję zmienić obraz za pomocą pinView! .image = UIImage (o nazwie: "GrayDot")! to nadal nie działa. – maxxxo

+3

Czy próbowałeś użyć 'MKAnnotationView' zamiast' MKPinAnnotationView'? – Para

+0

Właśnie zrobiłem to z MKAnnotationView – maxxxo

7

MKPinAnnotationView zawsze używaj zdjęcia pinezki, nie można tego robić. Zamiast tego należy użyć MKAnnotationView. Należy pamiętać, ponieważ właściwości animatesDrop to nie jest poprawna właściwość MKAnnotationView, Rem the line.