ios - 按位置和缩放级别向服务器请求 mapkit 注释

标签 ios annotations mkmapview mapkit

我有这个问题:

我有一个 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/

相关文章:

ios - 如何区分抓拍图像相册中选择的图像

iphone - iOS 6 上的 YouTube 播放器方向问题

ios - Sprite 套件尺寸变更

java - 有关 hibernate 复合模式的帮助

ios - MapView Pin 注释的问题 - 本地图缩放/平移/区域更改时 Pin 会失去颜色

iphone - 是否可以为 infoPlist.strings (应用程序名称)设置默认语言?

java - 基于 Hibernate 注解的查询执行错误?

symfony - 在 Symfony/Doctrine ORM 中生成实体 Getter 和 Setter

iphone - MKMapView 加载时覆盖除 View 之外的所有内容?仅限iOS5<

ios - Mapview nil 关闭,在 StackO 上看到了主题。但没有帮助我