MapView(iOS)中无法识别的错误 [英] Unrecognized Error in MapView (iOS)
问题描述
我在MapView中收到一个我无法识别但无法找到文档的错误。它看起来像这样:
I'm getting an error in MapView that I don't recognize and can't find documentation on. It looks like this:
CoreAnimation: ignoring exception:
Invalid Region <center:-180.00000000, -180.00000000
span:+2.81462803, +28.12500000>
显然这些数字现在是我的代码所独有但我无法弄清楚发生了什么。 MapView运行得很好,我的所有注释都会显示出来(它会像我设置的那样放大用户的位置)。具体到底是什么意思?
Obviously the numbers are exclusive to my code right now but I can't figure out what's going on. The MapView runs just fine and all of my annotations show up (and it zooms on the user's location like I have it set). What specifically does this point to?
谢谢。
这是我用来缩放到用户位置的方法。这有点不正统,但这是我得到帮助的原因,因为我出于各种原因遇到缩放问题(我可以解释一下,如果需要,但它可能不相关):
Here's the method I use to zoom to the user's location. It's a little unorthodox but it's what I've been helped with since I had problems with zooms for a variety of reasons (I can explain if need be, but it's probably not relevant):
- (void)zoomToUserLocation:(MKUserLocation *)userlocation
{
if (!userlocation)
return;
MKCoordinateRegion region;
region.center = userlocation.coordinate;
region.span = MKCoordinateSpanMake(2.0, 2.0);
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self zoomToUserLocation:self.mapView.userLocation];
}
- (void)mapView:(MKMapView *)theMapView didUpdateUserLocation:(MKUserLocation *)location
{
[self zoomToUserLocation:location];
}
推荐答案
不知道在哪里无效坐标来自但我建议将以下检查添加到 zoomToUserLocation
方法。
Can't tell where the invalid coordinates are coming from but I suggest adding the following checks to the zoomToUserLocation
method.
只检查是否 userlocation
nil
是不够的。您还必须检查 userlocation
中的 location
属性是否为零。 然后,您可以使用坐标
属性(特别是当您使用 didUpdateUserLocation $ c之外的坐标时$ c>委托方法)。
Just checking if userlocation
is nil
is not enough. You have to also check if the location
property inside userlocation
is nil or not. Then, you can use the coordinate
property (especially when you're using the coordinates outside the didUpdateUserLocation
delegate method).
另外,只检查坐标
是否 0,0
(技术上是有效坐标),因为如果结构从未设置过,结构将为零,或者甚至可以用随机数据填充结构。 Core Location框架的 CLLocationCoordinate2DIsValid
函数用作防止无效区域的最后一道防线。
Also, just checking if coordinate
is 0,0
(technically a valid coordinate) is not recommended as the struct will be "zero" if it's never been set or it could even be filled with random data. The Core Location framework's CLLocationCoordinate2DIsValid
function is used as the last line of defense to prevent an invalid region.
你可以如果需要,还要检查时间戳
和 horizontalAccuracy
。
You could also check the timestamp
and horizontalAccuracy
if you want.
- (void)zoomToUserLocation:(MKUserLocation *)userlocation
{
if (!userlocation)
return;
if (!userlocation.location)
{
NSLog(@"actual location has not been obtained yet");
return;
}
//optional: check age and/or horizontalAccuracy
//(technically should check if location.timestamp is nil first)
NSTimeInterval locationAgeInSeconds =
[[NSDate date] timeIntervalSinceDate:userlocation.location.timestamp];
if (locationAgeInSeconds > 300) //adjust max age as needed
{
NSLog(@"location data is too old");
return;
}
if (!CLLocationCoordinate2DIsValid(userlocation.coordinate))
{
NSLog(@"userlocation coordinate is invalid");
return;
}
MKCoordinateRegion region;
region.center = userlocation.coordinate;
region.span = MKCoordinateSpanMake(2.0, 2.0);
//region = [self.mapView regionThatFits:region];
//don't need to call regionThatFits explicitly, setRegion will do it
[self.mapView setRegion:region animated:YES];
}
此外(可能不相关,你可能已经这样做但是),基于您之前的几个与此相关的问题,您可能希望在地图视图控制器的 viewWillDisappear中清除并重新设置地图视图的
和委托
viewWillAppear
防止某些错误的方法:
Additionally (possibly unrelated and you may have already done this but), based on a couple of your previous questions related to this, you might want to clear and re-set the map view's delegate
in the map view controller's viewWillDisappear
and viewWillAppear
methods to prevent certain errors:
-(void)viewWillAppear:(BOOL)animated
{
mapView.delegate = self;
}
-(void)viewWillDisappear:(BOOL)animated
{
mapView.delegate = nil;
}
这篇关于MapView(iOS)中无法识别的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!