MKCoordinateRegionMakeWithDistance未在MapView上设置正确的区域 [英] MKCoordinateRegionMakeWithDistance does not set the correct region on the MapView

查看:1464
本文介绍了MKCoordinateRegionMakeWithDistance未在MapView上设置正确的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

   - (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(@用户位置: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(@到西边界的距离:%.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(@到西边界的距离:%.2lfm,[centerRegion distanceFromLocation:north]);
// -------------------------------------------- ----------------
}

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

 用户位置:lat:48.9793946200 long:2.4726272850 
distance set:636.9887048804 m

center:lat:48.97937199 long:2.47269630
到西部边界的距离:500.44m
到西部边界的距离:650.57m

设置dist = dist2时,表示:

 用户位置:lat :48.9793946200 long:2.4726272850 
distance set:900.8380655203m

center:lat:48.97937199 long:2.47269630
到西部边界的距离:500.44m
到西部边界的距离: 650.57m



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



最后问题:如何确保在地图上显示所需的米,

解决方案

如果我理解正确,你想要的是水平和垂直视觉告诉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);

>已通过:0.005728 0.008702
>已通过2:0.005728 0.008702
>设置:0.012957 0.013733

如果你碰到你的距离达1200米,你将能够看到下一个缩放级别。



BTW,您的代码中有一个小错误:

  NSLog(@到西边界的距离:%.2lfm,[centerRegion distanceFromLocation:north]); 

应为

  NSLog(@到北边界的距离:%.2lfm,[centerRegion distanceFromLocation:north]); 


I have this code :

- (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]);
    // ------------------------------------------------------------
}

When setting dist = dist1, that gives :

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

When setting dist = dist2, that gives :

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

What's the problem here ? Why do I have the same display with 2 different distances ?

Final question : How can I be sure to display the wanted meters on the map, at minimum for horizontal and vertical visual (with or without animation of course) ?

解决方案

If I understand it correctly, you want to tell the mapView "give me a map which is 636m across" or "give me a map which is 900m across". But the map gives you the same distance across for both of these.

When you set a map region, you generally don't get back exactly what you ask for, but instead a best fit. The map view looks at the region you requested, then creates a region which fits your region inside it. The problem is that the map view doesn't zoom exactly to your requested region, it finds the highest zoom level that allows all your region to be visible. It won't use an in-between zoom level. This is why when you use setRegion: the map always looks crisp. You can manually set an in-between zoom level by pinching in or out, but not (as far as I know) programatically.

Note too that the map view might change the actual region variable you pass to it. Here's the docs:

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.)

You can see the difference in regions by logging the region you give it, and the one the map view actually sets (although I didn't see the passed myRegion change):

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

If you bump your distance up to 1200m, you'll be able to see the next zoom level.

BTW, there's a small error in your code:

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

should be

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

这篇关于MKCoordinateRegionMakeWithDistance未在MapView上设置正确的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆