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

时间:2015-06-08 11:33:33

标签: ios swift mapkit quickblox

我正在使用QuickBlox,我的地图会根据用户的位置进行更新。

我正在获取用户的位置并使用“QBRequest.geoDataWithFilter”功能将它们放在地图上。 我正在创建一个具有半径值的过滤器。我还使用mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool)函数来检测区域何时发生变化。

用户的位置会定期更新,并根据用户的位置(登录的用户)而不是可见区域从服务器接收,因此我不关心中心的地图。

我希望能够在缩小时加载更多用户,因此每次用户缩小时半径应该增加,并且在他或她放大的情况下减小

如何使用地图的跨度计算地图上可见区域的半径? (如果可能,我只需要等式)

提前致谢。

1 个答案:

答案 0 :(得分:2)

它不是半径所需要的。

您需要使用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];

其中span可以计算为

- (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;
}