从XML / Array创建地图引脚

时间:2015-08-16 17:42:52

标签: ios xml swift mapkit

我目前正在开发一个项目,该项目从XML文件中抓取一堆交通摄像头,并在应用程序中显示它们。我已经启动并运行了Tableview,工作正常(单击一个单元格,打开detailView并显示流量图像)。但是,我想在地图上显示cameralocations,因此用户可以按下他们想要看到相机的图钉(并将它们发送到detailView以显示相机)。

我不确定如何使这项工作,任何想法?

我得到了经度和纬度坐标。链接到XML文件:http://webkamera.vegvesen.no/metadata

它是挪威语,但是lengdegrad =经度和breddegrad =纬度。

这就是我想要实现的目标(Photoshop截图):https://gyazo.com/93d885606efd6e6369018243b64d47e8

我不知道这是否是合适的地方,但如果您知道某事,请提供帮助: - )

提前致谢

1 个答案:

答案 0 :(得分:0)

使用XML数据:

将XML文件添加到项目中并使用NSXMLParser进行阅读。

要在地图上创建注释:

基于MKAnnotationView创建一个注释类,类似于:

class Annotation: NSObject, MKAnnotation {
    var coordinate: CLLocationCoordinate2D
    var title: String?
    // optionally add subtitle

    init(coordinate: CLLocationCoordinate2D, title: String) {
        self.coordinate = coordinate
        self.title = title

    } 
}

要创建anotations并将其添加到地图,请为XML文件中的每个已解析项创建一个注释并将其添加到地图中。这样的事情,取决于您解析数据的格式。我们假设您将数据放在名为cameras的数组中:

for camera in cameras {
    let coord = CLLocationCoordinate2DMake(camera.latitude, camera.longitude)
    let annotation = Annotation(coordinate: coord, title: camera.title)                      
    mapView.addAnnotation(annotation)
}   
相关问题