【问题标题】:Swift adding annotations on map快速在地图上添加注释
【发布时间】:2016-09-13 11:59:01
【问题描述】:

试图在地图上做一些注释。但绕不过去。我使用的代码是

 // Names
 names = ["Ben", "Big", "Hawk", "Enot", "Wiltons", "Scott's", "The Laughing"]

        // Latitudes, Longitudes
        coordinates = [
            [51.519066, -0.135200],
            [51.513446, -0.125787],
            [51.465314, -0.214795],
            [51.507747, -0.139134],
            [51.509878, -0.150952],
            [51.501041, -0.104098],
            [51.485411, -0.162042],
            [51.513117, -0.142319]
        ]

【问题讨论】:

  • 这看起来就像一个数据结构。有什么尝试将注释实际添加到您的地图视图中?您如何创建地图视图?
  • 您需要显示任何尝试。你用的是什么地图?
  • 地图视图跟随用户位置(创建他的方式:youtube.com/watch?v=qrdIL44T6FQ)但我只想将这些作为注释放在地图上。我尝试使用字符串,但什么也没有发生.. var names:[String]!变量描述:[字符串]!变量坐标:[AnyObject]! var currentRestaurantIndex: Int = 0 var Locationmanager = CLLocationManager()

标签: ios swift annotations


【解决方案1】:

函数 addAnnotation 接受一个 CLLocation 类型的数组,这是您应该使用的。所以让数组看起来像这样来初始化 CLLocation 对象。我假设你已经有一个可以工作的渲染地图、mapView 对象等。

let coords = [  CLLocation(latitude: xxxx, longitude: xxxx),
   CLLocation(latitude: xxx, longitude: xxx),
   CLLocation(latitude: xxx, longitude:xxx)
    ];

这里有一个函数可以获取该数组,并循环遍历每个元素并将其作为注释添加到 mapView(尚未渲染)

func addAnnotations(coords: [CLLocation]){
        for coord in coords{
            let CLLCoordType = CLLocationCoordinate2D(latitude: coord.coordinate.latitude,
                                                      longitude: coord.coordinate.longitude);
            let anno = MKPointAnnotation();
            anno.coordinate = CLLCoordType;
            mapView.addAnnotation(anno);
        }

    }

最后,您需要使用MKMapViewDelegate 委托来使用其自动调用的方法之一,当添加新注释时,我们可以对其进行排队和渲染。如果您只添加一次注释,这将是不必要的,但最好像这样添加它们,以便您以后可以操作它们。

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation is MKUserLocation{
        return nil;
    }else{
        let pinIdent = "Pin";
        var pinView: MKPinAnnotationView;
        if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinIdent) as? MKPinAnnotationView {
            dequeuedView.annotation = annotation;
            pinView = dequeuedView;
        }else{
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: pinIdent);

        }
        return pinView;
    }
}

不要只是添加此代码并期望它能够工作,请确保将其正确地合并到项目的正确区域中。如果您还有其他问题,请发表评论。

更新:

记得将MKMapViewDelegate 协议继承到你使用的控制器中。

确保您在ViewDidLoad 中实际调用addAnnotations,并通过coords 数组。可以在ViewDidLoad中定义。

确保 mapView 方法不在 ViewDidLoad 中,而是控制器的成员。

【讨论】:

  • 我使用了代码并将值更改为正确的值。但是我得到了(从未使用过不可变值“坐标”)。我将第一个代码放在 viewdidload 中,是对的还是我完全迷路了?
  • 你需要调用函数addAnnotation并将数组作为参数传入。
  • 我将更新我的答案,列出要记住的事项,告诉我进展如何。
  • 如何在 ViewDidLoad 中调用 addAnnotations?
  • 什么意思?就叫它……?
【解决方案2】:

添加标题为 Swift 5.0 的简单注释

    let addAnotation = MKPointAnnotation()
    addAnotation.title = "YOUR TITLE"
    addAnotation.coordinate = CLLocationCoordinate2D(latitude: [YOUR LATITIUDE], longitude: [YOUR LONGITUDE])
    self.mapView.addAnnotation(addAnotation)

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
    guard annotation is MKPointAnnotation else { return nil }

    let identifier = "Annotation"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: identifier)

    if annotationView == nil {
        annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
        annotationView!.canShowCallout = true
    } else {
        annotationView!.annotation = annotation
    }

    return annotationView
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多