视图如何与注释相关/松散耦合?我得到了Annotationsarray,我需要的观点

时间:2017-09-06 23:55:16

标签: swift macos mapkit mkannotationview mapkitannotation

SryImNew : (假设我想显示gliphXYZ,如果标题=="主页")

"注释视图松散地耦合到相应的注释对象" https://developer.apple.com/documentation/mapkit/mkannotationview

如何松散地结合它们,在哪里? 或者我已经自动拥有多个MKAnnotationView,如果是这样,如何解决它们&他们的财产?

我的案例

我已经开始运行:一个应用程序,可以显示地图上各个位置的多个图钉 由

rawArrayWithFormat=[[namestring,lat,long][namestring,lat...]]

class Place: NSObject, MKAnnotation {
      var title: String?
      var subtitle: String?
      var latitude: Double
      var longitude:Double

      var coordinate: CLLocationCoordinate2D {
      return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
      }

      init(latitude: Double, longitude: Double) {
      self.latitude = latitude
      self.longitude = longitude
} 

通过遍历rawArray返回[Place]数组的函数

func getMapAnnotations() -> [Place] {
var annotations:Array = [Place]()    

for item in rawArray {

    let lat = (item[1] as AnyObject) as! Double
    let long = (item[2] as AnyObject) as! Double
    let annotation = Place(latitude: lat, longitude: long)


    annotation.title = (item[0] as AnyObject) as? String
    annotations.append(annotation)
} 
return annotations

然后在 ViewController

@IBOutlet weak var mapView: MKMapView!
...

最后我调用覆盖 func viewDidLoad {

         mapView.addAnnotations(getMapAnnotations())

0 个答案:

没有答案