来自MKCoordinateForMapPoint的意外结果

时间:2016-01-26 04:23:42

标签: swift2

在下面的代码片段中,我通过调用MKCoordinateForMapPoint获得了意想不到的结果。

let firstPoint = MKPointAnnotation()
let firstPointLocation = MKMapPointMake(41.8735923, -87.6764638) 
print("firstPointLocation: \(firstPointLocation)")
// Prints: firstPointLocation: MKMapPoint(x: 41.8735923, y:-87.6764638)
let firstPointCoordinates = MKCoordinateForMapPoint(firstPointLocation)
print("firstPointCoordinates: \(firstPointCoordinates)") 
// Prints: firstPointCoordinates: CLLocationCoordinate2D(latitude: 85.0511389233241, longitude: -179.999943843137)
firstPoint.coordinate = firstPointCoordinates
print("firstPoint.coordinate: \(firstPoint.coordinate)")
// Prints: firstPoint.coordinate: CLLocationCoordinate2D(latitude: 85.0511389233241, longitude: -179.999943843137)
firstPoint.title = "Cook County Hospital"
mapView.addAnnotation(firstPoint)

正如您将从上面第二个print语句的输出中看到的,对MKCoordinateForMapPoint的调用将坐标转换为其他内容。因此,这一点并未出现在预期的位置。如果我用下面的那个替换MKCoordinateForMapPoint行,那么该点将正确显示。

firstPoint.coordinate = CLLocationCoordinate2D(latitude: 41.8735923, longitude: -87.6764638)
print("firstPoint.coordinate: \(firstPoint.coordinate)") 
// Prints: firstPoint.coordinate: CLLocationCoordinate2D(latitude: 41.8735923, longitude: -87.6764638)

我检查了MKCoordinateForMapPoint的引用,它应该接受一个类型MKMapPoint并将其转换为CLLocationCoordinate2D类型:

func MKCoordinateForMapPoint(_ mapPoint: MKMapPoint) -> CLLocationCoordinate2D

你知道我为什么没有在这里得到预期的结果吗?

1 个答案:

答案 0 :(得分:0)

代码正在完全按照预期执行,但是您滥用MKMapPointMake

MKMapPointMake从CGPoint(相对于其框架)创建一个点,而不是从lat / lng创建一个点。

我认为问题的根源只是混乱,或者是愚蠢的蠢事。考虑重命名变量,使它们不那么相似。