MKMapView显示错误保存的区域 [英] MKMapView show incorrectly saved region

查看:117
本文介绍了MKMapView显示错误保存的区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的iPhone应用程序关闭时,我将地图区域保存到用户默认值中:

  MKCoordinateRegion region = mapView。地区; 
[[NSUserDefaults standardUserDefaults] setDouble:region.center.latitude forKey:@map.location.center.latitude];
[[NSUserDefaults standardUserDefaults] setDouble:region.center.longitude forKey:@map.location.center.longitude];
[[NSUserDefaults standardUserDefaults] setDouble:region.span.latitudeDelta forKey:@map.location.span.latitude];
[[NSUserDefaults standardUserDefaults] setDouble:region.span.longitudeDelta forKey:@map.location.span.longitude];

当应用程序再次启动时,Ш以相同的方式读取这些值,以便用户可以准确地看到与上次相同的地图视图:

  MKCoordinateRegion region; 

region.center.latitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@map.location.center.latitude];
region.center.longitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@map.location.center.longitude];
region.span.latitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@map.location.span.latitude];
region.span.longitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@map.location.span.longitude];

NSLog([NSString stringWithFormat:@Region read:%f%f%f%f,region.center.latitude,region.center.longitude,region.span.latitudeDelta,region.span .longitudeDelta]);

[mapView setRegion:region];

NSLog([NSString stringWithFormat:@地图上的区域:%f%f%f%f,mapView.region.center.latitude,mapView.region.center.longitude,mapView.region。 span.latitudeDelta,mapView.region.span.longitudeDelta]);

我从用户默认值中读取的区域(毫不奇怪)与保存时完全相同。请注意,保存的内容直接来自地图,因此不会以任何方式进行转换。我用 setRegion:方法将其设置回地图,但后来却不同了!



示例结果:

 区域读数:50.241110 8.891555 0.035683 0.042915< br> 
地图上的地图:50.241057 8.891544 0.050499 0.054932

有人知道为什么会这样吗?

解决方案

这里的问题是当您设置区域时,地图缩放级别捕捉到最接近的缩放阈值。 (我怀疑这些缩放阈值是双击或双指敲击时获得的缩放量)



因此,如果地图显示缩放级别1实例,并将区域设置为相同的跨度值: region = [mapView region]; [mapView setRegion:region]; 它将捕捉到最近的缩放级别高于级别1,即级别2,你将缩小大约两倍。



原始海报的解决方法是在设置区域之前稍微减小范围值,这样当视图突然显示时,它会突然缩小到缩放级别是,而不是上面那个。



例如



region.span.latitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@map.location.span.latitude] * 0.999;



region.span.longitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@map.location.span.longitude] * 0.999;



如果用户已经使用双击进行缩放(因此从阈值跳到阈值)这非常有效,几乎完全将它们返回到相同的视图。



但是如果他们捏缩放,视图在缩放阈值之间,它将是sti我会突然跳到下一个级别。在这种情况下不太好,但目前还没有解决。



Apple雷达上存在开放式漏洞,希望它将在未来版本中修复。 / p>

I'm saving map region into user defaults when my iPhone app is closing like this:

MKCoordinateRegion region = mapView.region;
[[NSUserDefaults standardUserDefaults] setDouble:region.center.latitude forKey:@"map.location.center.latitude"];
[[NSUserDefaults standardUserDefaults] setDouble:region.center.longitude forKey:@"map.location.center.longitude"];
[[NSUserDefaults standardUserDefaults] setDouble:region.span.latitudeDelta forKey:@"map.location.span.latitude"];
[[NSUserDefaults standardUserDefaults] setDouble:region.span.longitudeDelta forKey:@"map.location.span.longitude"];

When app launches again, Ш read those values back the same way, so that the user can see exactly the same map view as it was last time:

MKCoordinateRegion region;

region.center.latitude  = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.center.latitude"];
region.center.longitude = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.center.longitude"];
region.span.latitudeDelta  = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.latitude"];
region.span.longitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.longitude"];

NSLog([NSString stringWithFormat:@"Region read  : %f %f %f %f", region.center.latitude, region.center.longitude, region.span.latitudeDelta, region.span.longitudeDelta]);

[mapView setRegion:region];

NSLog([NSString stringWithFormat:@"Region on map: %f %f %f %f", mapView.region.center.latitude, mapView.region.center.longitude, mapView.region.span.latitudeDelta, mapView.region.span.longitudeDelta]);

The region I read from user defaults is (not surprisingly) exactly the same as when it was saved. Notice that what is saved comes directly from the map, so it's not transformed in any way. I set it back on map with setRegion: method, but then it is different!

Example results:

Region read  : 50.241110 8.891555 0.035683 0.042915<br>
Region on map: 50.241057 8.891544 0.050499 0.054932

Does anybody know why this happens?

解决方案

The issue here is when you set the region, the map zoom level "snaps" out to the nearest zoom threshold. (I suspect these zoom thresholds are the amounts of zoom you get when you double-tap or two-finger-tap)

So if the map is showing zoom level 1 for instance, and you set the region to that same span value thusly: region = [mapView region]; [mapView setRegion:region]; it will "snap" out to the nearest zoom level above level 1, i.e. level 2 and you will zoom out by about a factor of two.

The workaround for the original poster is to reduce the span values slightly before setting the region, so that when the view snaps out, it snaps out to the zoom level it was on, not the one above.

e.g.

region.span.latitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.latitude"] * 0.999;

region.span.longitudeDelta = [[NSUserDefaults standardUserDefaults] doubleForKey:@"map.location.span.longitude"] * 0.999;

If the user had been zooming with double-taps (and hence jumping from threshold to threshold) this works pretty well, returning them to the same view almost exactly.

However if they pinch-zoom and the view is halfway between the zoom thresholds it will still snap out to the next level. Not so good in that case but there is no fix as yet.

There are open bugs on Apple radar for this, hopefully it will be fixed in a future release.

这篇关于MKMapView显示错误保存的区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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