【问题标题】:Get center coordinates from MapKit and display in UILabel从 MapKit 获取中心坐标并在 UILabel 中显示
【发布时间】:2015-12-20 04:57:02
【问题描述】:

我正在使用 XCode 7.2 和 Swift 2.1 开发 iOS 应用,并且我已经在我的应用中成功实现了 MapKit 地图。地图加载完美,并以用户当前位置为中心。

现在我想在用户将地图中心移动到新位置时检索中心坐标。这个新位置的坐标将在按下按钮时被“捕获”(参见附件 gif 中的“设置网格参考”按钮)并显示在标签中。

Move from A to B and set new center coordinates

我最接近答案的是here,我已经实现了该代码,但我仍然不知道如何通过单击我创建的 IBOutlet 按钮来使用坐标更新标签.

我在这里错过了什么?!

任何帮助将不胜感激 - 提前致谢!

----------------后续问题------------------

既然我们已经解决了这个问题,并且我们已经用新的中心坐标填充了标签,我还有一个问题 - 很可能是新手疏忽,但我在这里处于陡峭的学习曲线上,所以请耐心等待我……

我们已成功确定中心坐标,现在它们在我们创建的函数中设置为 ma​​pLongitudema​​pLatitude

我还有另外两个变量(newTargetLongnewTargetLat),它们构成将传递给下一个视图控制器的值数组的一部分,我想:

let newTargetLong = mapLongitude
let newTargetLat = mapLatitude

以便可以使用.append 指令将新的经纬度添加到数组中。

但由于某种原因,我无法从该函数中“取出”这两个值。我需要做什么才能做到这一点?

【问题讨论】:

    标签: ios swift mapkit


    【解决方案1】:

    在类的顶部声明var center = "" 和其他声明。在下面的方法中,它应该会自动改变center的值:

    func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        center = mapView.centerCoordinate
    }
    

    当您按下按钮时,将标签的值设置为中心值。

    self.yourLabelName.text = center
    

    要显示为“纬度:...经度:...”,请执行以下操作:

    func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        let mapLatitude = mapView.centerCoordinate.latitude
        let mapLongitude = mapView.centerCoordinate.longitude
        center = "Latitude: \(mapLatitude) Longitude: \(mapLongitude)"
        print(center)
        self.yourLabelName.text = center
    }
    

    如果你想格式化坐标以显示更友好一点:

    center = "\(String(format: "%.5f", mapLatitude)), \(String(format: "%.5f", mapLongitude))"
    

    根据您偏好的小数位数调整 %.5f。

    【讨论】:

    • 谢谢,@app-dev-guy 快速提问。将 center 声明为变量时,它有什么类型?我已按照您的指示进行操作,并且我得到的标签填充了以下内容:CLLocationCoordinate2D(latitude: 0.0, longitude: 0.0)
    • @JacoGriesel 将尽快回复。
    • @JacoGriesel 答案不正确,我们会尽快回复您。
    • @JacoGriesel 感谢您的耐心等待。请看上面。我测试过,它可以工作。
    • 仍在努力获得那些能够获得支持的声誉...... :(但一旦我拥有它,我会回到这里!;-)
    【解决方案2】:

    好的,所以我对@AppDevGuy 提供的代码进行了一些小改动。这是我所做的:

    func mapView(myMap: MKMapView, regionDidChangeAnimated animated: Bool) {
    
        let center = myMap.centerCoordinate
    
        let mapLatitude = center.latitude
        let mapLongitude = center.longitude
        let latAndLong = "Lat: \(mapLatitude) \nLong: \(mapLongitude)"
    
        self.TargetGridReference.text = latAndLong
    }
    

    对于按钮按下:

    @IBAction func SetTargetGridReference(sender: UIButton) {
    
        return mapView(myMap, regionDidChangeAnimated: true)
    }
    

    UILabel 中的输出如下所示:

    Lat: -34.5678901234567
    Long: 19.1234567890123
    

    我不确定这是笨重还是优雅,但它就像一个魅力!我检查了输出,坐标是正确的!

    由此产生的最后一个问题:有没有办法将这些纬度和经度值缩短为 6 位,而不是目前的 13 位?

    感谢@AppDevGuy!由衷赞赏!

    【讨论】:

    • 感谢您的支持 :-)
    【解决方案3】:

    首先我假设:您不确定何时何地更新标签的文本(您创建)

    然后,根据您关于如何获取当前中心坐标的链接帖子, 我觉得你可以试试在这个好玩的地方加一句:

     func mapView(mapView: MKMapView, regionDidChangeAnimated animated:     Bool) {
    var center = mapView.centerCoordinate
    #add a line here to update the label
    } 
    

    由于每次用户重新定位地图时,都会调用这个委托函数,并且标签会自动更新为当前新的中心坐标。

    【讨论】:

    • 不,它不起作用,@david-hu。有什么建议吗?
    • 尝试在这个函数中打印一些东西,看看它是否真的被调用了
    • 如果在您重新定位地图时正在调用此函数,并且应该更新标签
    【解决方案4】:

    要缩短坐标,请使用“%.4f”格式。这是我用于经纬度的一根绳子的例子,带有一个缩短的坐标,我相信你可以弄清楚它是如何工作的。

    myLabel.text = "\(String(format: "%.4f", location.coordinate.latitude)), \(String(format: "%.4f", location.coordinate.longitude))"
    

    这会将我所在位置的坐标打印到一个标签中。 .4 代表小数位。所以你可以选择你想要的任何数字,你想要的坐标有多长或多短。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-23
      • 1970-01-01
      相关资源
      最近更新 更多