开发者

Map is displayed only if I wait at least 1 minute

I have view1 in which there is button1, when the user click on button 1, the current location of the user is tracked and a second view view2 is shown where the user set his search parameters and click on button2, when button2 clicked, a view view3 is shown where a Map is displayed.

Now my problem is if the user wait 1 minute at least before clicking on button2, the Map is shown pretty well, otherwise the Map is not displayed. My relevant code for view2 is :

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

    }

my code in view3 which make problem :

-(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];
    TopStati开发者_开发知识库onAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication      sharedApplication]delegate];
    latitudeOfUserLocation=topStation.latitudeUtilisateur;
    longitudeOfUserLocation=topStation.longitudeUtilisateur;
}

Why should I wait 1 minute, how can I solve this?


I am assuming you are trying to show the user location based on the code provided. You have created a CLLocationManager instance and asked it to update the user's current location. This is bound to take some time before the delegate method is called. You set the app delegate's topStation.latitudeUtilisateur and topStation.longitudeUtilisateur in the delegate method.

Suppose the user moves on to the next screen before these values are retrieved, the values above wouldn't be set. You accessed the data in the -viewWillAppear before they are set and hence would get stale or nil data. Trying to show this location would not work properly.

If you really wish to display the user's location, can't you afford to useMKMapView's showsUserLocation. Set this to YES and handle the location update in the delegate's -mapView:didUpdateUserLocation: method.

Let me know if I misunderstood your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜