簡體   English   中英

Swift:如何將 CGPoint 轉換為 CLLocationCoordinate2D

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

我有以下代碼,並且正在嘗試為地圖上的長按添加注釋。 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)

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM