Swift 在地图上添加注释 [英] Swift adding annotations on map

查看:30
本文介绍了Swift 在地图上添加注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Trying to make some annotations on the map. But can't get around it. the code I'm using is

 // 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]
        ]

推荐答案

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

The function addAnnotation takes an array of type CLLocation, which is what you should use. So make the array look like this to initialize CLLocation objects. I assume you already have a working rendered map, mapView object, etc.

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

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

here is a function that can take that array, and loops through each element and adds it as an annotation to the mapView (not yet rendered)

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 委托来使用其自动调用的方法之一,当添加新注释时,我们可以排队并渲染它.如果您只添加一次注释,这将是不必要的,但最好像这样添加它们,以便您以后可以对其进行操作.

Finally, you need to use the MKMapViewDelegate delegate to use one of its automatically called methods, when a new annotation is added, so we can queue and render it. This would be unnecessary if you were adding annotations once, but it is good to have them added like this so you can manipulate them later.

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

不要只是添加此代码并期望它起作用,请确保在项目的正确区域正确合并它.如果您有其他问题,请发表评论.

Do not just add this code and expect it to work, make sure you incorporate it correctly, in the right areas of your project. Do comment if you have further questions.

更新:

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

remember to inherit the MKMapViewDelegate protocol into the controller your using.

确保您在 ViewDidLoad 中实际调用了 addAnnotations,并传递了 coords 数组.可以在 ViewDidLoad 中定义.

Make sure you actually call addAnnotations, in ViewDidLoad, and pass through coords array. Which can be defined in ViewDidLoad.

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

Make sure the mapView method is not in ViewDidLoad but a member of the controller.

这篇关于Swift 在地图上添加注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆