ios - 如何计算 MKMapView 中可见区域的半径?

标签 ios swift mapkit quickblox

我正在使用 QuickBlox,并且我有一张可以根据用户位置更新的 map 。

我正在使用“QBRequest.geoDataWithFilter”函数获取用户的位置并将其放置在 map 上。 我正在创建一个具有半径值的过滤器。我还使用 mapView(mapView: MKMapView!,regionDidChangeAnimatedanimated: Bool) 函数来检测区域何时更改。

用户的位置会定期更新,并且它们是根据用户的位置(登录的用户)从服务器接收的不是可见区域,所以我不关心中心 map 的。

我希望能够在缩小时加载更多用户,因此每次用户缩小时半径都应增加,而在放大时半径应减小

如何使用 map 的跨度计算 map 上可见区域的半径? (如果可能的话,我只需要方程)

提前致谢。

最佳答案

它不是所需的半径。

您需要使用mapView 中的region 参数。

查看苹果文档,其中非常清楚。

完成本教程。会对你有很大帮助

icode blog mapkit demo

具体来说,你需要设置这样的东西..

MKCoordinateSpan span = [self coordinateSpanWithMapView:self centerCoordinate:centerCoordinate andZoomLevel:zoomLevel];
MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);
[self setRegion:region animated:animated];

其中跨度可以计算为

- (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView
                         centerCoordinate:(CLLocationCoordinate2D)centerCoordinate
                             andZoomLevel:(NSUInteger)zoomLevel
{
// convert center coordiate to pixel space
double centerPixelX = [self longitudeToPixelSpaceX:centerCoordinate.longitude];
double centerPixelY = [self latitudeToPixelSpaceY:centerCoordinate.latitude];

// determine the scale value from the zoom level
NSInteger zoomExponent = 20 - zoomLevel;
double zoomScale = pow(2, zoomExponent);

// scale the map’s size in pixel space
CGSize mapSizeInPixels = mapView.bounds.size;
double scaledMapWidth = mapSizeInPixels.width * zoomScale;
double scaledMapHeight = mapSizeInPixels.height * zoomScale;

// figure out the position of the top-left pixel
double topLeftPixelX = centerPixelX - (scaledMapWidth / 2);
double topLeftPixelY = centerPixelY - (scaledMapHeight / 2);

// find delta between left and right longitudes
CLLocationDegrees minLng = [self pixelSpaceXToLongitude:topLeftPixelX];
CLLocationDegrees maxLng = [self pixelSpaceXToLongitude:topLeftPixelX + scaledMapWidth];
CLLocationDegrees longitudeDelta = maxLng - minLng;

// find delta between top and bottom latitudes
CLLocationDegrees minLat = [self pixelSpaceYToLatitude:topLeftPixelY];
CLLocationDegrees maxLat = [self pixelSpaceYToLatitude:topLeftPixelY + scaledMapHeight];
CLLocationDegrees latitudeDelta = -1 * (maxLat - minLat);

// create and return the lat/lng span
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
return span;
}

关于ios - 如何计算 MKMapView 中可见区域的半径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30708073/

相关文章:

android - 使用应用通知 facebook 为我的应用邀请新用户

ios - 将位置数据 POST 到接收多个条目的服务器

ios - 如何使用 Swift 从警报中的按钮单击返回 bool

ios - Swift 中的 MKTileoverlay 问题

ios - 当WatchKit应用程序/扩展唤醒时,我可以在父级iOS应用程序中调试代码吗?

ios - 标签栏动画

ios - 如何绘制NSString atPoint withAttributes 并调整SIZE?

swift - tableview 单元格不显示来自类的文本

iPhone 当前用户位置坐标显示为 (0,0)

swift - mapView中firebase的下载地址