简体   繁体   English

如何在MapView中快速添加多个注释或图钉

[英]How to add multiple Annotations or Pins in MapView in swift

I am developing an application which locates the current user location and displays the nearest sport centers to the user's current location. 我正在开发一个应用程序,该应用程序可以找到当前用户的位置并显示距用户当前位置最近的运动中心。 At the moment I am adding the latitude and longitude manually into the viewDidLoad() . 目前,我正在将纬度和经度手动添加到viewDidLoad()

override func viewDidLoad() {

    super.viewDidLoad()
    let pinLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(43.0757,25.6172)
    let objectAnn = MKPointAnnotation()
    objectAnn.coordinate = pinLocation
    objectAnn.title = "Fitnes"
    objectAnn.subtitle = "Power"
    self.MapView.addAnnotation(objectAnn)

    let pinLocation2: CLLocationCoordinate2D = CLLocationCoordinate2DMake(43.0657,25.6272)
    let objectAnn2 = MKPointAnnotation()
    objectAnn2.coordinate = pinLocation2
    objectAnn2.title = "Swimming Pool"
    objectAnn2.subtitle = "Aqua"
    self.MapView.addAnnotation(objectAnn2)
}

Is it possible to save the latitude and longitude coordinates in a JSON file and when the user opens the app, they are uploaded automatically into the function? 是否可以将纬度和经度坐标保存在JSON文件中,并且当用户打开应用程序时,它们会自动上传到函数中?

Also is it possible out of the coordinations the application to calculate the distance, so that the user is able to set a (km) range and see only these places which are near him? 应用程序是否还可以通过协调来计算距离,从而使用户能够设置(公里)范围并仅查看他附近的这些地方?

I would be glad if you provide me with an simple code. 如果您为我提供一个简单的代码,我将非常高兴。

You asked: 您询问:

Is it possible to save the latitude and longitude coordinates in a JSON file and when the user opens the app, they are uploaded automatically into the function? 是否可以将纬度和经度坐标保存在JSON文件中,并且当用户打开应用程序时,它们会自动上传到函数中?

Yes, build a dictionary with the latitude and longitude values and then use NSJSONSerialization to convert back and forth between that dictionary and a NSData that can be saved to (and read from) a file. 是的,使用纬度和经度值构建字典,然后使用NSJSONSerialization在该字典和可以保存到文件(或从文件中读取)的NSData之间来回转换。 If this is just for your own purposes, you can also read and write NSDictionary values directly to a file, without needing JSON. 如果这仅出于您自己的目的,您还可以直接将NSDictionary值读写到文件中,而无需JSON。 JSON is excellent, though, if you're going to be sending and receiving this data with external resources, such as a web service. 但是,如果要使用外部资源(例如Web服务)发送和接收此数据,则JSON非常有用。

Also is it possible out of the coordinations the application to calculate the distance, so that the user is able to set a (km) range and see only these places which are near him? 应用程序是否还可以通过协调来计算距离,从而使用户能够设置(公里)范围并仅查看他附近的这些地方?

Sure, create CLLocation objects from the CLLocationCoordinate2D values. 当然,请从CLLocationCoordinate2D值创建CLLocation对象。 And then you can use distanceFromLocation to calculate the distance between two CLLocation objects. 然后,您可以使用distanceFromLocation来计算两个CLLocation对象之间的距离。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM