如何找到可见的MKMapView屏幕区域的半径?

3

我想知道iPhone屏幕上可见区域的半径,因为当我缩放时,可见区域会发生变化,所以我想知道那个特定区域的半径,我该怎么做?


你解决了这个问题吗?让我知道,我有同样的情况。 - Pooja Shah
2个回答

4

所需的不是半径。

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

查看苹果文档,从那里可以很清楚地了解。

通过这个教程,它会帮助你很多。

icode博客mapkit演示

具体来说,你需要设置类似于这样的内容...

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

干杯 :)


我有不同的情况,我需要发送区域半径值,然后服务器将返回该区域内办公室的数据,所以我可能需要半径值,否则就必须用不同的方式更改XML。 - Chatar Veer Suthar
哎,老兄。当你改变区域和跨度时,请再次发送纬度经度参数,服务器将返回相应于这些区域的对象。可以参考谷歌的逆地理编码API,它的工作方式与此类似。 - SeriousSam
是的,但我正在使用的 Web 服务需要半径,所以我能否从当前可见区域的经纬度获取半径? - Chatar Veer Suthar
通常情况下,地图区域是一个矩形。因为我们都使用谷歌地图,所有访问机制都遵循相同的规则。关于您的Web服务,我不知道您是否需要与您的服务提供商咨询。 - SeriousSam
不回答问题。 - cloudsurfin
显示剩余3条评论

0
我可能误解了问题,但这难道不是简单的吗:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    CGFloat latD = mapView.region.span.latitudeDelta;
    CGFloat lngD = mapView.region.span.longitudeDelta;
    NSLog(@"This is the latitude delta of the visible map: %f", latD);
    NSLog(@"This is the longitude delta of the visible map: %f", lngD);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接