2014-11-18 18 views
7

Tôi muốn biết nếu có ai có thể cho tôi biết cách tôi có thể chạm vào một pin trên map dưới dạng MKPointAnnotations.MKPointAnnotations sự kiện cảm ứng trong swift

Tôi muốn nhấp vào pin trên map và chuyển đến chế độ xem khác bằng cách mang lại variables của số pin mà tôi đã đặt trước.

Có ai có thể giải thích cho tôi điều này trong số Swift không?

nhờ

Sửa với mã:

class ViewController: UIViewController, MKMapViewDelegate { 


@IBOutlet weak var mappa: MKMapView! 


override func viewDidLoad() { 
    super.viewDidLoad() 

    var location : CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 44.648590, longitude: 10.918794) 

    let pinAnnotation = PinAnnotation() 
    pinAnnotation.setCoordinate(location) 

    self.mappa.addAnnotation(pinAnnotation) 

} 

class PinAnnotation : NSObject, MKAnnotation { 
    private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0) 

    var coordinate: CLLocationCoordinate2D { 
     get { 
      return coord 
     } 
    } 

    var title: String = "test" 
    var subtitle: String = "test" 

    func setCoordinate(newCoordinate: CLLocationCoordinate2D) { 
     self.coord = newCoordinate 
    }   
} 

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
    if annotation is PinAnnotation { 
     let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") 

     pinAnnotationView.pinColor = .Purple 
     pinAnnotationView.draggable = true 
     pinAnnotationView.canShowCallout = true 
     pinAnnotationView.animatesDrop = true 

     let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton 
     deleteButton.frame.size.width = 44 
     deleteButton.frame.size.height = 44 
     deleteButton.backgroundColor = UIColor.redColor() 
     deleteButton.setImage(UIImage(named: "trash"), forState: .Normal) 

     pinAnnotationView.leftCalloutAccessoryView = deleteButton 


     return pinAnnotationView 
    } 

    return nil 
} 

func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) { 
    if let annotation = view.annotation as? PinAnnotation { 
     self.mapView.removeAnnotation(annotation) 
    } 
} 
} 
+0

Nếu tiêu đề của chú thích là trống, callout sẽ không hiển thị khi bạn gõ vào nó. – Anna

+0

Tôi đã thêm chú thích và văn bản viết, cửa sổ bật lên xuất hiện chính xác nhưng không có gì .. bạn biết tại sao? –

+0

Có thể cửa sổ đại biểu của chế độ xem bản đồ không được kết nối/đặt. – Anna

Trả lời

20

vài bước là cần thiết, sau đây là một số đoạn mã để giúp bạn bắt đầu.

Trước tiên, bạn cần một lớp tùy chỉnh cho chú thích pin của bạn chứa dữ liệu bạn muốn làm việc.

import MapKit 
import Foundation 
import UIKit 

class PinAnnotation : NSObject, MKAnnotation { 
    private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0) 

    var coordinate: CLLocationCoordinate2D { 
     get { 
      return coord 
     } 
    } 

    var title: String = "" 
    var subtitle: String = "" 

    func setCoordinate(newCoordinate: CLLocationCoordinate2D) { 
     self.coord = newCoordinate 
    }   
} 

Sau đó, bạn cần một lớp tùy chỉnh cho số MKMapView của bạn tuân theo giao thức MKMapViewDelegate. Thực hiện phương pháp viewForAnnotation có:

import MapKit 
import CLLocation 
import Foundation 
import UIKit 

class MapViewController: UIViewController, MKMapViewDelegate { 

    ... 

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
     if annotation is PinAnnotation { 
      let pinAnnotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "myPin") 

      pinAnnotationView.pinColor = .Purple 
      pinAnnotationView.draggable = true 
      pinAnnotationView.canShowCallout = true 
      pinAnnotationView.animatesDrop = true 

      let deleteButton = UIButton.buttonWithType(UIButtonType.Custom) as UIButton 
      deleteButton.frame.size.width = 44 
      deleteButton.frame.size.height = 44 
      deleteButton.backgroundColor = UIColor.redColor() 
      deleteButton.setImage(UIImage(named: "trash"), forState: .Normal) 

      pinAnnotationView.leftCalloutAccessoryView = deleteButton 

      return pinAnnotationView 
     } 

     return nil 
    } 

    func mapView(mapView: MKMapView!, annotationView view: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) { 
     if let annotation = view.annotation as? PinAnnotation { 
      mapView.removeAnnotation(annotation) 
     } 
    } 

Cung cấp cho bạn một cái gì đó như thế này:

enter image description here

Để thêm chú thích mới vào bản đồ của bạn sử dụng một nơi nào đó trong mã của bạn:

let pinAnnotation = PinAnnotation() 
pinAnnotation.setCoordinate(location) 

mapView.addAnnotation(pinAnnotation) 
+0

Xin chào, trong viewDidLoad để tải điểm trên bản đồ như vách ngăn? Tôi không thể tìm ra cách để cho bản đồ thêm ghim –

+0

Tôi đã chỉnh sửa câu trả lời của mình để cho biết cách thêm một ghim mới vào bản đồ. – zisoft

+0

Tôi đã chỉnh sửa câu hỏi của mình bằng mã của mình, bây giờ tôi thấy ghim trên bản đồ, nhưng nếu tôi nhấp không làm gì cả, không có chú giải công cụ nào xuất hiện, làm cách nào để khắc phục? –

3

Greate work !!! NHƯNG .. Tôi chỉ cần sao chép quá khứ này và tôi đã phải thêm một vài thay đổi. Tôi sẽ chia sẻ với các bạn những thay đổi đó.

import MapKit 
import Foundation 
import UIKit 

class PinAnnotation : NSObject, MKAnnotation { 
private var coord: CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 0, longitude: 0) 
private var _title: String = String("") 
private var _subtitle: String = String("") 

var title: String? { 
    get { 
     return _title 
    } 
    set (value) { 
     self._title = value! 
    } 
} 

var subtitle: String? { 
    get { 
     return _subtitle 
    } 
    set (value) { 
     self._subtitle = value! 
    } 
} 

var coordinate: CLLocationCoordinate2D { 
    get { 
     return coord 
    } 
} 

func setCoordinate(newCoordinate: CLLocationCoordinate2D) { 
    self.coord = newCoordinate 
} 
} 

Tôi hy vọng nó sẽ giúp: D

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