我有这个问题:
我有一个 API 服务器来请求最近的位置(纬度和经度)和距离(公里)
当用户在 map 上进行平移时,我会调用此 API。所以我会根据缩放级别计算距离参数。
我怎样才能得到这个?
此刻我有这个 MapKit 委托(delegate)方法:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
MKZoomScale currentZoomScale = mapView.bounds.size.width / mapView.visibleMapRect.size.width;
NSLog(@"currentZoom:%f", currentZoomScale);
[self.dataReader sendServerRequestWithCoordinate:mapView.region.center andDistance:[self getDistanceByZoomLevel:currentZoomScale];
}
- (float) getDistanceByZoomLevel:(MKZoomScale) zoomLevel {
/// ?????? ////
}
最佳答案
您不需要根据“缩放级别”或“缩放比例”计算距离。
Map Kit 和 Core Location 具有计算给定坐标或 map 点的距离的方法和功能。
假设您要使用当前可见 map 所覆盖的对角线距离(从左上角到右下角)。
角坐标(CLLocationCoordinate2D
s)可以从 map View 的 region
中获得。属性,然后您可以使用 distanceFromLocation
方法来计算两个坐标之间的距离。
角落 map 点(MKMapPoint
s)可以从 map View 的visibleMapRect
中获得。属性,然后您可以使用 MKMetersBetweenMapPoints
函数来获取它们之间的距离。
这是使用 map 点的示例:
MKMapRect vmr = mapView.visibleMapRect;
//vmr.origin is the top-left corner MKMapPoint
MKMapPoint bottomRightMapPoint =
MKMapPointMake(vmr.origin.x + vmr.size.width,
vmr.origin.y + vmr.size.height);
CLLocationDistance distanceMeters =
MKMetersBetweenMapPoints(vmr.origin, bottomRightMapPoint);
当然,如果你想要公里而不是米,除以
distanceMeters
1000.0。另外,如果通过“距离”你实际上想要半径(到中心点的距离),那么还要除以
distanceMeters
2.0。
关于ios - 按位置和缩放级别向服务器请求 mapkit 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22001907/