如何从 swift 的地图视图中的特定点获取经度和纬度?
how can I fetch the longitude and latitude from a specific point on my mapview in swift?
我的应用程序中有一个 MKMapView
,目前我正在使用以下方法获取这张地图的中心点:
longitude = mapView.centerCoordinate.longitude
latitude = mapView.centerCoordinate.latitude
但现在我不想取地图的中心点,而是取地图视图顶部边缘下方 100 像素的特定点。所以通常我会使用类似的东西:
longitude = mapView.centerCoordinate.longitude
latitude = mapView.(centerCoordinate+100px).latitude
但这显然不是它的工作原理。有什么方法可以从 mkmapview 中获取特定点吗?
使用了MapKit方法:
convertPoint:toCoordinateFromView:
试试这个:
let frame = mapView.frame
let myPoint = CGPointMake(frame.midX, 100)
let myCoordinate = mapView.convertPoint(myPoint, toCoordinateFromView: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = myCoordinate
annotation.title = "My Point"
mapView.addAnnotation(annotation)
我的应用程序中有一个 MKMapView
,目前我正在使用以下方法获取这张地图的中心点:
longitude = mapView.centerCoordinate.longitude
latitude = mapView.centerCoordinate.latitude
但现在我不想取地图的中心点,而是取地图视图顶部边缘下方 100 像素的特定点。所以通常我会使用类似的东西:
longitude = mapView.centerCoordinate.longitude
latitude = mapView.(centerCoordinate+100px).latitude
但这显然不是它的工作原理。有什么方法可以从 mkmapview 中获取特定点吗?
使用了MapKit方法:
convertPoint:toCoordinateFromView:
试试这个:
let frame = mapView.frame
let myPoint = CGPointMake(frame.midX, 100)
let myCoordinate = mapView.convertPoint(myPoint, toCoordinateFromView: mapView)
let annotation = MKPointAnnotation()
annotation.coordinate = myCoordinate
annotation.title = "My Point"
mapView.addAnnotation(annotation)