gpt4 book ai didi

ios - Swift:如何将 CGPoint 转换为 CLLocationCoordinate2D

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:44 25 4
gpt4 key购买 nike

我有以下代码,正在尝试为 map 上的长按添加注释。 locationInView 的类型为“CGPoint”,而“annotation.coordinate”需要 CLLocationCoordinate2D 类型的变量。将 CGPoint 转换为 CLLocationCoordinate2D 的正确方法是什么?

 func myGestureFunc(thegesture: UIGestureRecognizer) {

let pointOfInterest = thegesture.locationInView(self.theMap) //CGPoint


let annotation = MKPointAnnotation()
annotation.coordinate = //expects CLLocationCoordinate2D


theMap.addAnnotation(annotation)

}

最佳答案

您应该按照以下方式使用 convertPoint:

let touchPoint = thegesture.locationInView(self.theMap)
let newCoordinate = self.theMap.convertPoint(touchPoint, toCoordinateFromView:self.theMap)
let annotation = MKPointAnnotation()
annotation.coordinate = newCoordinate
theMap.addAnnotation(annotation)

关于ios - Swift:如何将 CGPoint 转换为 CLLocationCoordinate2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35591011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com