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

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

问题描述

尝试在地图上做一些注释.但是无法绕开它.我正在使用的代码是

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.

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

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