异常:'无效区域
'当尝试显示地图时

发布于 2024-11-06 21:01:59 字数 3943 浏览 3 评论 0原文

当我尝试显示地图时,出现以下异常:

由于未捕获的异常“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];

}

编辑

我尝试按照您在评论中所说的方式进行操作。异常已解决,但是,当我尝试在控制台中显示用户的经度/纬度时,我什么也得不到。这是我的代码,大部分是正确的:

    -(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(设备)上测试我的代码,我注意到当我尝试搜索时,应用程序已成功跟踪我的位置并找到符合我搜索的电台(紫色注释)但是,地图并未显示,搜索进度仍在加载,永不停止。

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

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

screenshot

When I try to display the map I get this exception :

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:+inf, +0.00000000 span:+1.00000000, +0.50000000>'

My relevant code is this :

-(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];

}

Edit

I tried to do as you said in the comments. The exception is solved, however, when I try to display the longitude/latitude of the user in the console I get nothing. This is my code which is mostly correct:

    -(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

Even I work on the simulator, I should get something on the console right?

Edit

Hi again, i had the opportunity to test my code on iPhone (device) and i have noticed that when i try to search, the application has succeeded to track my position and to find me the stations that meet my search (purple annotation), however, the map isn't displayed and the searching progress is still loading and never stop.

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

Here is a screenshot that could explain better:

screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

星星的轨迹 2024-11-13 21:01:59

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

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

"Invalid Region" exception is through because the region you set to mapView is invalid. As I know now, there are 2 reasons can cause this exception: region's center point is invalid, or region's span is invalid.

To avoid this exception:
1) Check the center point value: latitude is in range [-90;90], longitude is in range [-180;180]
2) Use [mapView regionThatFits:region] to get a region with valid span then set to mapview:
[mapView setRegion:[mapView regionThatFits:region]]

月朦胧 2024-11-13 21:01:59

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

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

In some situations (when the app becomes active from background) didUpdateUserLocation method is fired, but without updated location. In these cases there is no valid region, and setRegion: method can throw an exception.
Stupid, a simple solution can be checking if its region is valid before you set it:

 if(region.center.longitude == -180.00000000){
    NSLog(@"Invalid region!");
}else{
    [aMapView setRegion:region animated:YES];
}
养猫人 2024-11-13 21:01:59

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

相反,实现 locationManager:didUpdateToLocation:fromLocation: 委托方法并读取其中的位置。

startUpdatingLocation 之后的所有代码移至该方法:

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

注意: 上面的方法已被贬值:
Apple Doc

After calling startUpdatingLocation, it may take a few seconds for the location to be updated so you can't try to retrieve it immediately afterwards. Until it is updated, location contains invalid values which is what the error tells you.

Instead, implement the locationManager:didUpdateToLocation:fromLocation: delegate method and read the location in there.

Move all the code after startUpdatingLocation to that method:

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

Note: above method is depriciated:
Apple Doc

泡沫很甜 2024-11-13 21:01:59
[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];
[mapView setRegion:[mapView regionThatFits:region] animated:TRUE];
写给空气的情书 2024-11-13 21:01:59

不能大于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);

Can not be greater than 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文