8

我想知道是否有人可以告诉我如何mapMKPointAnnotations.

我想单击pinmap通过带回我预设variables的来转到另一个视图。pin

任何人都可以向我解释这件事Swift吗?

谢谢

用代码编辑:

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)
    }
}
}
4

2 回答 2

24

需要几个步骤,这里有一些代码片段可以帮助您入门。

首先,您需要一个自定义类来保存您要使用的数据的 pin 注释。

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 coordinate: CLLocationCoordinate2D {
        get {
            return coord
        }
    }

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

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

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

然后你需要一个MKMapView符合MKMapViewDelegate协议的自定义类。在那里实现该方法viewForAnnotation

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)
        }
    }

这给了你这样的东西:

在此处输入图像描述

要向地图添加新注释,请在代码中的某处使用:

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

mapView.addAnnotation(pinAnnotation)
于 2014-11-18T10:36:24.833 回答
5

伟大的工作!但是..我只是复制过去,我不得不添加一些更改。我将与你们分享这些变化。

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
}
}

我希望它会有所帮助:D

于 2016-06-09T10:11:07.373 回答