从 XML/Array 创建地图图钉
Create Map Pins from XML/Array
我目前正在做一个项目,该项目从 XML 文件中抓取一堆交通摄像头,并在应用程序中显示它们。我已经启动了我的 Tableview 并且 运行,工作正常(单击一个单元格,打开 detailView 并显示交通图像)。但是,我想在地图上显示相机位置,以便用户可以按他们想要查看相机的图钉(并将它们发送到 detailView 以显示相机)。
我不确定如何进行这项工作,有什么想法吗?
我得到了经纬度坐标。 Link 到 XML 文件:http://webkamera.vegvesen.no/metadata
它是挪威语,但是 lengdegrad = 经度和 breddegrad = 纬度。
这就是我想要实现的(Photoshop 截图):https://gyazo.com/93d885606efd6e6369018243b64d47e8
我不知道这里问的对不对,如果有知道的请帮忙:-)
提前致谢
要使用 XML 数据:
将XML文件添加到您的项目中并使用NSXMLParser读取它。
要在地图上创建注释:
基于 MKAnnotationView 创建一个注解 class,类似于:
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
// optionally add subtitle
init(coordinate: CLLocationCoordinate2D, title: String) {
self.coordinate = coordinate
self.title = title
}
}
要创建注释并将其添加到地图,请为 XML 文件中的每个已解析项目创建一个注释并将其添加到地图。像这样的东西,取决于你解析的数据的格式。假设您的数据位于一个名为 cameras:
的数组中
for camera in cameras {
let coord = CLLocationCoordinate2DMake(camera.latitude, camera.longitude)
let annotation = Annotation(coordinate: coord, title: camera.title)
mapView.addAnnotation(annotation)
}
我目前正在做一个项目,该项目从 XML 文件中抓取一堆交通摄像头,并在应用程序中显示它们。我已经启动了我的 Tableview 并且 运行,工作正常(单击一个单元格,打开 detailView 并显示交通图像)。但是,我想在地图上显示相机位置,以便用户可以按他们想要查看相机的图钉(并将它们发送到 detailView 以显示相机)。
我不确定如何进行这项工作,有什么想法吗?
我得到了经纬度坐标。 Link 到 XML 文件:http://webkamera.vegvesen.no/metadata
它是挪威语,但是 lengdegrad = 经度和 breddegrad = 纬度。
这就是我想要实现的(Photoshop 截图):https://gyazo.com/93d885606efd6e6369018243b64d47e8
我不知道这里问的对不对,如果有知道的请帮忙:-)
提前致谢
要使用 XML 数据:
将XML文件添加到您的项目中并使用NSXMLParser读取它。
要在地图上创建注释:
基于 MKAnnotationView 创建一个注解 class,类似于:
class Annotation: NSObject, MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
// optionally add subtitle
init(coordinate: CLLocationCoordinate2D, title: String) {
self.coordinate = coordinate
self.title = title
}
}
要创建注释并将其添加到地图,请为 XML 文件中的每个已解析项目创建一个注释并将其添加到地图。像这样的东西,取决于你解析的数据的格式。假设您的数据位于一个名为 cameras:
的数组中for camera in cameras {
let coord = CLLocationCoordinate2DMake(camera.latitude, camera.longitude)
let annotation = Annotation(coordinate: coord, title: camera.title)
mapView.addAnnotation(annotation)
}