【问题标题】:How can I add multiple annotations on a map using GPS coordinates with Swift?如何使用 Swift 的 GPS 坐标在地图上添加多个注释?
【发布时间】:2016-02-29 01:43:34
【问题描述】:

我是 Swift 的新手。我正在按照教程在地图上进行注释。我成功地做了一个注释,就教程而言……我一直在寻找如何使用 Swift 进行多个注释但找不到它。

如何在地图上添加多个注释? 谢谢。

这是我来自 viewcontroller.swift 的代码:

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var map: MKMapView!  
    override func viewDidLoad() {
        super.viewDidLoad()


        //setting the map region
        let latitude: CLLocationDegrees = 33.606800
        let longitude: CLLocationDegrees = -111.845360
        let latDelta: CLLocationDegrees = 0.01
        let lonDelta: CLLocationDegrees = 0.01
        let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
        let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
        map.setRegion(region, animated: true)

        //map annotation
        let annotation = MKPointAnnotation()
        annotation.coordinate = location
        annotation.title = "Taliesin West"
        annotation.subtitle = "Design"
        map.addAnnotation(annotation)        

        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

【问题讨论】:

  • 将此作为起点。 Adding multiple annotaions 你也可以查看这个Tutorial。这也是一个一分钟的搜索来找到这些。请试一试,然后显示您的代码不起作用。
  • 感谢您的回复。这看起来与我所拥有的非常不同。
  • 我应该将其粘贴到我拥有的内容上,还是应该为每个注释执行此操作,或者将其添加到我当前拥有的代码中?我计划做的下一个注释是:Fallingwater Latitude 39.906299 Longitude -79.467794
  • 我的下一个问题是您的注释将来自哪里?有几种不同的方法可以做到这一点。还有哪个非常不同?我给了你两个不同的链接。两者都旨在为您提供解决此问题的不同方法。
  • 我使用谷歌地图获得了我的注释坐标。我更详细地查看了这两个示例,了解了 plist,然后做了教程版本。唯一阻止应用程序运行的是向 MapKit 添加注释步骤下的第一行代码。 func getMapAnnotations() -> [Station] { // 错误信息是:一行上的连续声明必须用';'分隔, 函数声明体中应为“{”,并且应为声明。

标签: ios swift gps annotations swift2


【解决方案1】:

只需像示例一样创建另一个注释并调用

map.addAnnotation(another)

【讨论】:

  • 感谢您的回复。我计划做的下一个注释是:落水纬度 39.906299 经度 -79.467794。我目前不在电脑前。我正在努力解决的部分问题是第二个位置的纬度和经度。这是第二个注释的外观吗? let another = MKPointAnnotation() another.coordinate = locationTwo = (39.906299, -79.467794) another.title = "Fallingwater" another.subtitle = "Design" map.addAnnotation(another)
猜你喜欢
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多