iPhone - 在 iOS 3.x 上使用 CLLocation 管理器更新时不显示用户位置

发布于 2024-10-10 13:21:17 字数 1798 浏览 2 评论 0原文

我有一个地图视图,当用它的视图控制器将其推到屏幕上时,它会显示一个地图的位置 用户和其他一些自定义注释。当视图控制器被推到屏幕上时,它会调用一个函数 -(void)adduserLocation 来显示用户的位置。

我还有一个刷新按钮,它也调用 -(void)adduserLocation 并刷新所有注释和用户位置,但是当按下刷新按钮时,用户位置不会出现在地图上,即使所有相同的回调和更新都是如此正在注册(我可以看到收到了用户位置并进行了所有必要的回调)

我正在使用 :

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil; //for some reason this is only being returned when the view is pushed and the get
                //location method is called but not when the same location method is called     
                //while the view already exists.

 }

and:

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

        NSLog(@"User location: %f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
        currentRegionLat = newLocation.coordinate.latitude; 
        currentRegionLng = newLocation.coordinate.longitude;
        CLLocationCoordinate2D coord = {latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude};
        MKCoordinateSpan span = {latitudeDelta: 0.006, longitudeDelta: 0.006};
        MKCoordinateRegion region = {coord, span};
        [mView setRegion:region];
        [self getCardsInLocationLat:newLocation.coordinate.latitude andLng:newLocation.coordinate.longitude]; //this is a function which sets the annotations 

}

我不知道我可能做错了什么。请帮忙。谢谢。

*更新** 我已经设法找到问题的根源并解决它: 问题是我在进行位置更新之前删除了地图上的所有注释 并且由于某种原因,未添加用户位置注释。 所以我所做的只是删除所有非 [MKUserLocation class] 注释并留下 用户位置注释。这解决了我的问题,但我仍然认为要么有一些 这里有一种苹果错误,或者我做错了一些我还看不到的事情。

I have a mapview which when pushed with its view controller onto the screen shows the location of a
user and some other custom annotations. when the view controller is pushed to screen it calls a function -(void)adduserLocation which shows the location of the user.

I also have a refresh button which also calls -(void)adduserLocation and refreshes all of the annotations and the user location but when the refresh button is pressed the users location does not appear on the map even though all of the same callbacks and updates are being registered (I can see that the user location is received and all the necessary callbacks are made)

I am using :

-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:(id <MKAnnotation>)annotation {

if ([annotation isKindOfClass:[MKUserLocation class]])
    return nil; //for some reason this is only being returned when the view is pushed and the get
                //location method is called but not when the same location method is called     
                //while the view already exists.

 }

and:

 - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

        NSLog(@"User location: %f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
        currentRegionLat = newLocation.coordinate.latitude; 
        currentRegionLng = newLocation.coordinate.longitude;
        CLLocationCoordinate2D coord = {latitude: newLocation.coordinate.latitude, longitude: newLocation.coordinate.longitude};
        MKCoordinateSpan span = {latitudeDelta: 0.006, longitudeDelta: 0.006};
        MKCoordinateRegion region = {coord, span};
        [mView setRegion:region];
        [self getCardsInLocationLat:newLocation.coordinate.latitude andLng:newLocation.coordinate.longitude]; //this is a function which sets the annotations 

}

I have no idea what I may be doing wrong. Please help. Thanks.

*UPDATE**
I have managed to find the source of the problem and work around it:
The problem was that I was removing all the annotations on the map before doing a location update
and for some reason the user location annotation was not being added.
So what I did is simply remove only all the non [MKUserLocation class] annotations and left
the user location annotation. This solved my problem but I still think that either there is some
kind of apple bug here or I am doing something wrong which I cannot yet see.

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

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

发布评论

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

评论(1

冷了相思 2024-10-17 13:21:17

嘿,为什么不将 MKMapView 属性 showsUserLocation 设置为 YES 呢?

mapView.showsUserLocation = YES;

这样您就不必担心刷新位置了。

如果您不想将其用于节省电池或其他用途,我认为我们需要查看您的更多代码。你的代码似乎不是问题所在。您在控制台中看到任何错误吗?

Hey, why don't you just set the MKMapView property, showsUserLocation, to YES?

mapView.showsUserLocation = YES;

This way you wouldn't really need to worry about refreshing the location.

If you don't want to use it for battery saving purposes or something, I think we would need to see more of your code. The code that you have doesn't seem to be where the problem lies. Do you get any errors in the console?

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