如何将UIMapView置于MKOverlay中间

时间:2011-06-23 15:47:36

标签: iphone ios mkoverlay mkmapview

我正在使用以下代码(基于Apple WWDC 2010 TileMap示例)加载带有MKOverlay图像的UIMapView(这可能在viewDidLoad方法中):

NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Tiles"];
TileOverlay *overlay = [[TileOverlay alloc] initWithTileDirectory:tileDirectory];
[map addOverlay:overlay];

接下来,我将UIMapView地图置于此叠加层的中心。目前我正在做这个我的经纬度硬编码如下:

double myLatitude = ...;
double myLongitude = ...;
CLLocation *location = [[CLLocation alloc] initWithLatitude:myLatitude longitude:myLongitude];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 4000, 4000);
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
[map setRegion:adjustedRegion animated:YES];

这很好用。我试图按如下方式计算叠加的中心:

double x, y;

x = MKMapRectGetMidX([overlay boundingMapRect]);
y = MKMapRectGetMidY([overlay boundingMapRect]);
CLLocationCoordinate2D coord = MKCoordinateForMapPoint(MKMapPointMake(x, y));

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coord, 4000, 4000);
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
[map setRegion:adjustedRegion animated:YES];

但这不起作用。 MKCoordinateForMapPoint返回叠加层外的纬度/经度。我不明白为什么。

有人可以建议解决方案吗?

提前致谢...

0 个答案:

没有答案