【问题标题】:How can I calculate the radius of the region visible in a MKMapView?如何计算 MKMapView 中可见区域的半径?
【发布时间】:2015-06-08 19:37:40
【问题描述】:

我正在使用 QuickBlox,并且我有一张随用户位置更新的地图。

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

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

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

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

提前致谢。

【问题讨论】:

  • 是的,我见过这个问题,但问题是我需要以米为单位的半径。这就是 geoDataWithFilter 函数的工作原理。有可能还是我应该寻找其他方法?

标签: ios swift mapkit quickblox


【解决方案1】:

需要的不是半径。

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

【讨论】:

    猜你喜欢
    • 2015-02-08
    • 2011-04-01
    • 2022-11-21
    • 2017-10-09
    • 2023-04-06
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 2019-03-24
    相关资源
    最近更新 更多