MKCoordinateRegionMakeWithDistance未在MapView上设置正确的区域

Oli*_*ver 6 iphone cocoa-touch google-maps mkmapview ios

我有这个代码:

- (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]);
    // ------------------------------------------------------------
}
Run Code Online (Sandbox Code Playgroud)

设置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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

设置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
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

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

最后一个问题:我怎样才能确保在地图上显示想要的水表,至少水平和垂直视觉(当然有或没有动画)?

nev*_*ing 8

如果我理解正确,你想告诉mapView"给我一张横跨636米的地图"或"给我一张900米宽的地图".但是这张地图为你提供了相同的距离.

当您设置地图区域时,通常不会完全回复您的要求,而是最合适.地图视图会查看您请求的区域,然后创建一个适合您所在区域的区域.问题是地图视图不会精确缩放到您请求的区域,它会找到允许您所有区域可见的最高缩放级别.它不会使用中间缩放级别.这就是为什么当你使用setRegion:地图时总是看起来很清晰.您可以通过挤入或缩小手动设置中间缩放级别,但不能(以我所知)以编程方式设置.

另请注意,地图视图可能会更改region传递给它的实际变量.这是文档:

设置新区域时,地图可以调整region参数中的值,使其精确地适合地图的可见区域.这是正常的,可以确保region属性中的值始终反映地图的可见部分.但是,它确实意味着如果在调用此方法后立即获得该属性的值,则返回的值可能与您设置的值不匹配.(您可以使用regionThatFits:方法来确定地图实际设置的区域.)

您可以通过记录您提供的区域以及地图视图实际设置的区域来查看区域的差异(尽管我没有看到传递的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
Run Code Online (Sandbox Code Playgroud)

如果你的距离超过1200米,你将能够看到下一个缩放级别.

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

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

应该

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