gpt4 book ai didi

iphone - MKCoordinateRegionMakeWithDistance 没有在 MapView 上设置正确的区域

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

我有这段代码:

- (void)viewDidLoad
{
[super viewDidLoad];

CLLocationCoordinate2D userLocation = CLLocationCoordinate2DMake(48.9793946200, 2.4726272850);
CLLocationDistance dist1 = 636.9887048804;
CLLocationDistance dist2 = 900.8380655203;
CLLocationDistance dist = dist1;

[self.myMapView setRegion:MKCoordinateRegionMakeWithDistance(userLocation, dist, dist) animated:YES];

// TEST
// ------------------------------------------------------------
MKCoordinateRegion region = self.myMapView.region;
CLLocationDegrees lat = region.center.latitude;
CLLocationDegrees lon = region.center.longitude - region.span.longitudeDelta/2;
CLLocation *west = [[[CLLocation alloc] initWithLatitude:lat longitude:lon] autorelease];

NSLog(@"User location: lat : %.10lf long : %.10lf", userLocation.latitude, userLocation.longitude);
NSLog(@"distance set: %.10lfm", dist);
NSLog(@"center: lat : %.8lf long : %.8lf", region.center.latitude, region.center.longitude);

CLLocation* centerRegion = [[[CLLocation alloc] initWithLatitude:region.center.latitude longitude:region.center.longitude] autorelease];
NSLog(@"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:west]);

lat = region.center.latitude - region.span.latitudeDelta/2 ;
lon = region.center.longitude;
CLLocation *north = [[[CLLocation alloc] initWithLatitude:lat longitude:lon] autorelease];

NSLog(@"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:north]);
// ------------------------------------------------------------
}

当设置 dist = dist1 时,会给出:

User location: lat : 48.9793946200 long : 2.4726272850
distance set: 636.9887048804m

center: lat : 48.97937199 long : 2.47269630
distance to western boundary: 500.44m
distance to western boundary: 650.57m

enter image description here

当设置 dist = dist2 时,会给出:

User location: lat : 48.9793946200 long : 2.4726272850
distance set: 900.8380655203m

center: lat : 48.97937199 long : 2.47269630
distance to western boundary: 500.44m
distance to western boundary: 650.57m

enter image description here

这里有什么问题?为什么我有相同的显示器但有 2 个不同的距离?

最后一个问题:我如何才能确保在 map 上至少显示水平和垂直视觉(当然有或没有动画)所需的仪表?

最佳答案

如果我理解正确,您想告诉 mapView“给我一张横跨 636 米的 map ”或“给我一张横跨 900 米的 map ”。但是 map 为您提供了这两者的相同距离。

当您设置 map 区域时,您通常不会准确返回您要求的内容,而是最适合的。 map View 查看您请求的区域,然后在其中创建一个适合您所在区域的区域。问题是 map View 不会精确缩放到您请求的区域,它会找到允许您所有区域可见的最高缩放级别。它不会使用中间缩放级别。这就是为什么当您使用 setRegion: 时 map 总是看起来清晰。您可以通过捏合或捏合来手动设置中间缩放级别,但不能(据我所知)以编程方式设置。

另请注意, map View 可能会更改您传递给它的实际 region 变量。这是文档:

When setting a new region, the map may adjust the value in the region parameter so that it fits the visible area of the map precisely. This is normal and is done to ensure that the value in the region property always reflects the visible portion of the map. However, it does mean that if you get the value of that property right after calling this method, the returned value may not match the value you set. (You can use the regionThatFits: method to determine the region that will actually be set by the map.)

您可以通过记录您提供的区域和 map View 实际设置的区域来查看区域的差异(尽管我没有看到传递的 myRegion 更改):

MKCoordinateRegion myRegion = MKCoordinateRegionMakeWithDistance(userLocation, dist, dist);
NSLog(@"Passed: %f %f", myRegion.span.latitudeDelta, myRegion.span.longitudeDelta);
[self.mapView setRegion:myRegion animated:YES];

NSLog(@"Passed 2: %f %f", myRegion.span.latitudeDelta, myRegion.span.longitudeDelta);
NSLog(@"Set: %f %f", mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta);

> Passed: 0.005728 0.008702
> Passed 2: 0.005728 0.008702
> Set: 0.012957 0.013733

如果您将距离提高到 1200 米,您将能够看到下一个缩放级别。

顺便说一句,您的代码中有一个小错误:

NSLog(@"distance to western boundary: %.2lfm", [centerRegion distanceFromLocation:north]);

应该是

NSLog(@"distance to northern boundary: %.2lfm", [centerRegion distanceFromLocation:north]);

关于iphone - MKCoordinateRegionMakeWithDistance 没有在 MapView 上设置正确的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7299747/

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