仅当我等待至少 1 分钟时才会显示地图

发布于 2024-11-08 07:06:11 字数 2037 浏览 1 评论 0原文

我有 view1 其中有 button1,当用户单击 button 1 时,将跟踪用户的当前位置并显示第二个视图view2 显示用户设置搜索参数并单击 button2 的位置,当单击 button2 时,将显示视图 view3显示地图的地方会显示 。

现在我的问题是,如果用户在点击 button2 之前至少等待 1 分钟,地图就会显示得很好,否则地图不会显示。 我的 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 分钟,我该如何解决这个问题?

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];
    TopStationAppDelegate *topStation=(TopStationAppDelegate *)[[UIApplication      sharedApplication]delegate];
    latitudeOfUserLocation=topStation.latitudeUtilisateur;
    longitudeOfUserLocation=topStation.longitudeUtilisateur;
}

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

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

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

发布评论

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

评论(1

猫九 2024-11-15 07:06:11

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

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

如果你真的想显示用户的位置,你不能使用MKMapViewshowsUserLocation吗?将其设置为 YES 并在委托的 -mapView:didUpdateUserLocation: 方法中处理位置更新。

如果我误解了您的问题,请告诉我。

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.

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