当我尝试显示地图时,我得到了这个例外:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid Region <center:+inf, +0.00000000 span:+1.00000000, +0.50000000>'
Run Code Online (Sandbox Code Playgroud)
我的相关代码是这样的:
-(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];
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何解决这个问题,提前thx :)
编辑:
嗨再次,我尝试按照你说的做,异常得到解决,但是,当我尝试在控制台中显示用户的经度/纬度我什么都没有,这是我的代码是非常正确的:
-(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
Run Code Online (Sandbox Code Playgroud)
即使我在模拟器上工作,我应该在控制台上得到一些东西吗?
编辑: 嗨再次,我有机会在iPhone(设备)上测试我的代码,我注意到,当我尝试搜索时,应用程序已经过度追踪我的位置并找到符合我搜索的站点(紫色注释) )但是,地图不会显示,搜索进度仍在加载,永不停止.
hud.labelText=@"Recherche en cours..";// this never stop loading
Run Code Online (Sandbox Code Playgroud)
这是一个可以更好地解释的截图:

dor*_*kon 12
在某些情况下(当应用程序从后台变为活动状态时)会触发didUpdateUserLocation方法,但不会更新位置.在这些情况下,没有有效的区域,并且setRegion:方法可以抛出异常.愚蠢的,一个简单的解决方案是在设置之前检查其区域是否有效:
if(region.center.longitude == -180.00000000){
NSLog(@"Invalid region!");
}else{
[aMapView setRegion:region animated:YES];
}
Run Code Online (Sandbox Code Playgroud)
Cao*_*Loc 12
"无效区域"异常是通过,因为您设置为mapView的区域无效.据我所知,有两个原因可能导致此异常:区域的中心点无效,或区域的跨度无效.
要避免此异常:
1)检查中心点值:纬度在[-90; 90]范围内,经度在[-180; 180]范围内
2)使用[mapView regionThatFits:region]获取有效范围的区域然后设置为mapview:
[mapView setRegion:[mapView regionThatFits:region]]
小智 4
调用 后startUpdatingLocation,位置可能需要几秒钟的时间才能更新,因此您无法随后尝试立即检索它。在更新之前,位置包含无效值,这就是错误告诉您的内容。
相反,实现locationManager:didUpdateToLocation:fromLocation:委托方法并读取其中的位置。
将后面的 所有代码移至startUpdatingLocation该方法:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *location = [locationManager location];
//etc...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16323 次 |
| 最近记录: |