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(@"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
当设置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
这里有什么问题? 为什么我有两个不同的距离相同的显示?
最后的问题:我怎样才能确保在地图上显示想要的水表,至less水平和垂直视觉(当然有或没有animation)?
如果我理解正确,你想告诉mapView“给我一张636米的地图”或者“给我一张900米的地图”。 但是这个地图给了你两个相同的距离。
当你设置一个地图区域时,你一般不会回到你想要的东西,而是最合适的。 地图视图查看您请求的区域,然后创build一个适合您的区域的区域。 问题是地图视图不能完全放大到你要求的地区,它会find最高的缩放级别,让你所有的地区都可见。 它不会使用中间缩放级别。 这就是为什么当你使用setRegion:
地图总是看起来很脆。 您可以手动设置一个介于两者之间的缩放级别,但不是(按我所知)编程。
还要注意,地图视图可能会更改您传递给它的实际region
variables。 这里是文档:
设置新区域时,地图可能会调整区域参数中的值,以便精确地匹配地图的可见区域。 这是正常的,并且确保region属性中的值总是反映地图的可见部分。 但是,这意味着如果您在调用此方法后立即获得该属性的值,则返回的值可能与您设置的值不匹配。 (您可以使用regionThatFits:方法来确定地图实际设置的区域。)
你可以通过logging你给它的区域和地图视图实际设置的区域来看到区域的差异(尽pipe我没有看到通过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]);
造成这种情况的一个可能的原因是您指定的区域具有方形的宽高比,而您的MKMapView
可能具有矩形比例。
当你设置区域时, MKMapView
将不会像原来一样使用它,而是会修改它以便:
- 其纵横比对应于视图的纵横比
- 新的区域包含指定的一个
因此,如果您的视图的宽高比为2:1,那么西/东边界将距中心200米,而北/南边界距中心100米。
按照上面的方法设置区域后可以尝试一些东西:
MKCoordinateRegion region = self.mapView.region; CLLocationDegrees lat = region.center.latitude; CLLocationDegrees lon = region.center.longitude - region.span.longitudeDelta/2; CLLocation *west = [[CLLocation alloc] initWithLatitude:lat longitude:lon]; NSLog(@"distance to western boundary: %.2lfm", [userLocation distanceFromLocation:west]); lat = region.center.latitude + region.span.latitudeDelta/2 lon = region.center.longitude; CLLocation *north = [[CLLocation alloc] initWithLatitude:lat longitude:lon]; NSLog(@"distance to northern boundary: %.2lfm", [userLocation distanceFromLocation:north]);
其中一个应该是100米。 如果没有,我会有兴趣看看他们是什么。
PS上面的代码还没有经过testing。
- 如何确定GMSMarker是否位于GMSPolygon(iOS Google Map SDK)
- MapKit中的MapTypeStyle
- 无法使用Xcode 7和带有Podfile的GoogleMaps将IOS应用上传到iTunes
- 如何“在谷歌地图上显示我当前的位置,当我打开ViewController?”在Swift?
- Google Maps for iOS – 如何判断一个标记是否在屏幕的边界内?
- 我的位置button没有出现在视图中(Google maps SDK,ios)
- 像UBER一样在Google Map上移动GMSMarker
- 如何从JSON输出数据解码路由点?
- 如何处理同一个地址上的多个位置并将它们集群?