2011-05-18 27 views
0

我有view1,其中有button1,当button 1用户点击,用户的当前位置进行跟踪和第二视图view2所示,其中用户设置自己的搜索参数并点击button2,当点击button2时,将显示一个视图view3,其中显示一个地图。地图只显示如果我等待至少1分钟

现在我的问题是,如果用户至少等待1分钟,然后点击button2,地图显示得相当好,否则地图不会显示。 我对view2相关代码:

- (void)viewDidLoad { 
//when this view is loaded, the user current location is tracked 
     self.locationManager=[[CLLocationManager alloc]init]; 
     [locationManager setDelegate:self]; 
     [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters]; 
     [locationManager startUpdatingLocation]; 
    } 
    #pragma mark- 
    #pragma mark CLLocationManagerDelegate 
    -(void)locationManager:(CLLocationManager *)manager 
    didUpdateToLocation:(CLLocation *)newLocation 
    fromLocation:(CLLocation *)oldLocation 
    { 
     float latitude=newLocation.coordinate.latitude; 
     float longitude=newLocation.coordinate.longitude; 
     TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication sharedApplication]delegate]; 
     topStation.latitudeUtilisateur=latitude; 
     topStation.longitudeUtilisateur=longitude; 
     NSLog(@"%f",latitude); 
     NSLog(@"%f",longitude); 
     [locationManager stopUpdatingLocation]; 

    } 

我在view3这使问题代码:

-(void)viewWillAppear:(BOOL)animated 
{ 
//here is the problem: 
//if the user wait 1 minute before coming to this view, I mean before clicking on button2 
//this view shows the map pretty well, otherwise the map is not displayed and I got brown //screen 

    [mapView removeAnnotations:mapView.annotations]; 
    [mapView setMapType:MKMapTypeStandard]; 
    TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication  sharedApplication]delegate]; 
    latitudeOfUserLocation=topStation.latitudeUtilisateur; 
    longitudeOfUserLocation=topStation.longitudeUtilisateur; 
} 

我为什么要等待1分钟,我怎么能解决这个问题?

回答

0

我假设您正试图根据提供的代码显示用户位置。您已创建CLLocationManager实例并要求它更新用户的当前位置。在调用委托方法之前,这肯定需要一些时间。您可以在代理方法中设置应用代理的topStation.latitudeUtilisateurtopStation.longitudeUtilisateur

假设用户在检索这些值之前转到下一个屏幕,则不会设置上述值。您在设置数据之前访问了-viewWillAppear中的数据,因此数据会过时或nil。试图显示这个位置将无法正常工作。

如果你真的想显示用户的位置,你能不能用得起MKMapViewshowsUserLocation。将其设置为YES并处理代理的-mapView:didUpdateUserLocation:方法中的位置更新。

让我知道我是否误解了你的问题。