2015-03-18 20 views
7

Tôi đang cố gắng thay đổi hình ảnh pin trên MKMapView trong Swift, nhưng tiếc là nó không hoạt động. Bất kỳ ý tưởng gì tôi đang làm sai? Tôi đã thấy một số ví dụ ở đây, nhưng đã không làm việc.Thay đổi hình ảnh pin trên MKMapView trong 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 
    } 
} 

Ảnh GreenDot có sẵn và được sử dụng ở những nơi khác.

Trả lời

17

Đừng quên để thiết lập:

map.delegate = self 

Và chắc chắn rằng UIViewController bạn thực hiện các giao thức MKMapViewDelegate.
Nếu bạn quên thực hiện việc này, việc triển khai mapView:viewForAnnotation: của bạn sẽ không được gọi cho bản đồ của bạn.

Bên cạnh đó, có vẻ như pinView!.animatesDrop = true phá vỡ hình ảnh tùy chỉnh. Bạn phải đặt số này là false hoặc sử dụng MKAnnotationView (không có thuộc tính animatesDrop).

Xem this related question nếu bạn muốn triển khai hoạt ảnh thả tùy chỉnh.

+0

Ok tôi đã làm điều này, bây giờ tôi có thể thay đổi pinColor với pinView !pinColor = .Green, nhưng khi tôi cố gắng thay đổi hình ảnh bằng pinView! .age = UIImage (có tên: "GrayDot")! điều này vẫn không hoạt động .. – maxxxo

+3

Bạn đã thử sử dụng 'MKAnnotationView' thay vì' MKPinAnnotationView' chưa? – Para

+0

Tôi vừa làm nó với MKAnnotationView – maxxxo

7

MKPinAnnotationView luôn sử dụng hình ảnh ghim, không thể bao giờ được. Thay vào đó, bạn phải sử dụng MKAnnotationView. Hãy lưu ý vì thuộc tính animatesDrop nó không phải là một tài sản hợp lệ của MKAnnotationView, Rem dòng.

Các vấn đề liên quan