如何将 UIMapView 置于 MKOverlay 中间

发布于 2024-11-16 18:14:25 字数 1335 浏览 3 评论 0原文

我使用以下代码(基于 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 返回叠加层之外的纬度/经度。我不明白为什么。

有人能提出解决方案吗?

提前致谢...

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] initWithTileDirectory: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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文