简体   繁体   English

Swift:如何将 CGPoint 转换为 CLLocationCoordinate2D

[英]Swift: How do you convert CGPoint to CLLocationCoordinate2D

I have the below code, and am trying to add annotation for a longpress on the map.我有以下代码,并且正在尝试为地图上的长按添加注释。 The locationInView is of type 'CGPoint' whereas the 'annotation.coordinate' expects variable of type CLLocationCoordinate2D. locationInView 的类型为“CGPoint”,而“annotation.coordinate”需要类型为 CLLocationCoordinate2D 的变量。 whats the right method to convert CGPoint to CLLocationCoordinate2D ?将 CGPoint 转换为 CLLocationCoordinate2D 的正确方法是什么?

 func myGestureFunc(thegesture: UIGestureRecognizer) {

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


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


        theMap.addAnnotation(annotation)

}

You should use convertPoint along the lines of:您应该按照以下方式使用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)

由于 convertPoint() 方法不再可用,可以使用以下代码:

let coordinate = googleMapView.projection.coordinate(for: point)

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

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