开发者

How to center a UIMapView in the middle of a MKOverlay

I am using the following code (based on the Apple WWDC 2010 TileMap example) to load an UIMapView with a MKOverlay image (this could is in the viewDidLoad method):

NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Tiles"];
TileOverlay *overlay = [[TileOverlay alloc] i开发者_如何学GonitWithTileDirectory:tileDirectory];
[map addOverlay:overlay];

Next I was to center the UIMapView map in the center of this overlay. At the moment I am doing this my hard-coding the latitude and longitude as follows:

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

This works fine. I have tried to calculate the center of the overlay as follows:

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

But this does not work. The MKCoordinateForMapPoint returns a latitude/longitude outside the overlay. I don't understand why.

Can anyone suggest a solution?

Thanks in advance...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜