【问题标题】:Exception : 'Invalid Region <center:+inf, +0.00000000 span:+1.00000000, +0.50000000>' when trying to display the map例外:'无效区域 <center:+inf, +0.00000000 span:+1.00000000, +0.50000000>' 尝试显示地图时
【发布时间】:2011-08-24 23:53:28
【问题描述】:

当我尝试显示地图时出现此异常:

由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无效区域

我的相关代码是这样的:

-(void)viewWillAppear:(BOOL)animated
{   
    [mapView removeAnnotations:mapView.annotations];
    
    // locationManager update as location
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self; 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
    locationManager.distanceFilter = kCLDistanceFilterNone; 
    [locationManager startUpdatingLocation];
    CLLocation *location = [locationManager location];
    //Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];
    
    latitudeOfUserLocation=coordinate.latitude;
    longitudeOfUserLocation=coordinate.longitude;
    
    location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
    MyLocation *annotation=[[[MyLocation alloc]initWithName:@"You are here" distanceVersLaStation:@"" coordinate:location2D]autorelease];
    annotation.pinColor = MKPinAnnotationColorRed;  
    [mapView addAnnotation:annotation];
    
    MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
    MKCoordinateRegion region={location2D,span};
    [mapView setRegion:region];

}

编辑

我试着按照你在 cmets 中所说的去做。异常已解决,但是,当我尝试在控制台中显示用户的经度/纬度时,我什么也得不到。这是我的代码,大部分是正确的:

    -(void)viewWillAppear:(BOOL)animated
    {  
            
        [mapView removeAnnotations:mapView.annotations];
        
        // locationManager update as location
        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self; 
        locationManager.desiredAccuracy = kCLLocationAccuracyBest; 
        locationManager.distanceFilter = kCLDistanceFilterNone; 
        [locationManager startUpdatingLocation];
        NSURL *url=[NSURL         URLWithString:@"http://ipho.franceteam.org/ad_V1/stations/"];
    ASIFormDataRequest * request=[ASIFormDataRequest requestWithURL:url];
[request setPostValue:[NSNumber numberWithFloat:longitudeOfUserLocation] forKey:@"longitude"];
    [request setDelegate:self];
    [request startAsynchronous];
    MBProgressHUD *hud=[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.labelText=@"Recherche en cours..";// this never stop loading

    }

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *location = [locationManager location];
    //Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];
    latitudeOfUserLocation=coordinate.latitude;
    longitudeOfUserLocation=coordinate.longitude;
    NSLog(@"your latitude :%f",latitudeOfUserLocation);//here nothing is shown
    NSLog(@"your longitude :%f",longitudeOfUserLocation);// and here too
    location2D = (CLLocationCoordinate2D){ .latitude = latitudeOfUserLocation, .longitude = longitudeOfUserLocation };
    MyLocation *annotation=[[[MyLocation alloc]initWithName:@"Vous êtes ici" distanceVersLaStation:@"" coordinate:location2D]autorelease];
    annotation.pinColor = MKPinAnnotationColorRed;  //or red or whatever
    [mapView addAnnotation:annotation];
    //
    MKCoordinateSpan span={latitudeDelta:1,longitudeDelta:0.5};
    MKCoordinateRegion region={location2D,span};
    [mapView setRegion:region];

}//End function

即使我在模拟器上工作,我也应该在控制台上得到一些东西,对吧?

编辑

再次嗨,我有机会在 iPhone(设备)上测试我的代码,我注意到当我尝试搜索时,应用程序已成功跟踪我的位置并找到符合我搜索条件的电台(紫色annotation),但是,地图不显示,搜索进度仍在加载中,永远不会停止。

hud.labelText=@"Recherche en cours..";// this never stop loading

这是一个可以更好地解释的屏幕截图:

【问题讨论】:

  • 我发现了这个问题的问题。就是坐标的长度:CLLocationCoordinate2D coordinate2D = {26.6374655768,106.6458781604};

标签: ios mapkit


【解决方案1】:

在某些情况下(当应用从后台激活时)会触发 didUpdateUserLocation 方法,但不会更新位置。在这些情况下,没有有效的区域,并且 setRegion: 方法可以抛出异常。 愚蠢,一个简单的解决方案可以在设置之前检查其区域是否有效:

 if(region.center.longitude == -180.00000000){
    NSLog(@"Invalid region!");
}else{
    [aMapView setRegion:region animated:YES];
}

【讨论】:

  • 这种方式也可以,我们只需要检查无效区域,即 if(region.center.longitude == 0.00000000){ NSLog(@"Invalid region!"); }else{ [aMapView setRegion:region animated:YES]; }
  • 这对我不起作用,但我的地图视图中再次出现 nan 错误
【解决方案2】:

“Invalid Region”异常通过是因为你设置为mapView的区域无效。据我所知,有两个原因会导致这个异常:区域的中心点无效,或者区域的跨度无效。

为避免此异常:
1)检查中心点值:纬度在[-90;90]范围内,经度在[-180;180]范围内
2) 使用 [mapView regionThatFits:region] 获取具有有效跨度的区域,然后设置为 mapview:
[mapView setRegion:[mapView regionThatFits:region]]

【讨论】:

  • 你如何检查中心点是否在你提到的范围内?
  • if ( (region.center.latitude &gt;= -90) &amp;&amp; (region.center.latitude &lt;= 90) &amp;&amp; (region.center.longitude &gt;= -180) &amp;&amp; (region.center.longitude &lt;= 180) { [mapView setRegion:[mapView regionThatFits:region]] }
  • 有一个很好的辅助函数:if(CLLocationCoordinate2DIsValid(CLLocationCoordinate2D coord))
【解决方案3】:

调用startUpdatingLocation 后,位置可能需要几秒钟才能更新,因此您无法在之后立即尝试检索它。在更新之前,位置包含无效值,这就是错误告诉您的内容。

改为实现locationManager:didUpdateToLocation:fromLocation: 委托方法并读取其中的位置。

将所有代码 startUpdatingLocation 移至该方法:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *location = [locationManager location];
    //etc...
}

注意:上述方法已被贬低:

【讨论】:

  • 嗨 Anna,感谢您的有用回答,感谢您,我已经解决了异常问题,但现在我尝试在控制台上显示经度/纬度的值以确保我在正确的道路但是我什么都没有,你能帮我吗?我已经编辑了我的帖子,谢谢 :)
  • 您的意思是它显示 0,0 还是根本没有消息?你等了几秒钟? 添加此方法,如果您从中看到任何内容,请告诉我:- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"didFailWithError: %@", error); }
  • 嗨,好吧,什么都没有显示,甚至没有显示消息didFailWithError:,我已经编辑了viewWillAppear方法的代码,我怀疑它没有调用委托方法来更新位置它直接将请求发送到网络服务而不等待用户位置更新,你怎么看?我可能错了:)
  • 好吧,您不能在获得用户位置之前调用 Web 服务(在 didUpdateToLocation 中)。但这不应该阻止 locationManager 获取位置。现在删除/注释掉 Web 服务调用。无论如何,您都需要将其移至 didUpdateToLocation。
  • 好的,我等了大约 30 秒才能看到 faiError 消息:2011-05-13 20:42:13.130 TopStation[2125:207] didFailWithError: Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)",请问这是什么意思?是因为模拟器的东西吗?
【解决方案4】:
[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];

【讨论】:

    【解决方案5】:

    不能大于 180

    CLLocationDegrees delta = MAX(maxLatitude - minLatitude, maxLongitude - minLongitude) * 1.1;
    
    delta = points.count < 2 ? 0.5 : delta;
    delta = MIN(delta, 180);
    
    CLLocationDegrees centerX = (maxLatitude-minLatitude)/2.0 + minLatitude;
    CLLocationDegrees centerY = (maxLongitude-minLongitude)/2.0 + minLongitude;
    
    CLLocationCoordinate2D theCoordinate = {centerX,centerY};
    MKCoordinateSpan span = MKCoordinateSpanMake(delta, delta);
    self.mapView.region = MKCoordinateRegionMake(theCoordinate, span);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多