gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 01:40:02 25 4
gpt4 key购买 nike

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

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

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

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

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

提前致谢。

最佳答案

要求的不是半径。

您需要使用 mapView 中的区域参数。

查看 apple 文档,从中很清楚。

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com