用户位置Mapkit ViewDidLoad [英] User Location Mapkit ViewDidLoad

查看:95
本文介绍了用户位置Mapkit ViewDidLoad的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,从我在这里看到的多个答案中,这是如何在应用加载且效果很好时居中和缩放用户位置的方法.

So from the multiple answers I see on here this is how to center and zoom on user location when the app loads and it works great.

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    NSLog(@"Did update user location");
    MKCoordinateRegion mapRegion;
    mapRegion.center = mapView.userLocation.coordinate;
    mapRegion.span.latitudeDelta = 0.2;
    mapRegion.span.longitudeDelta = 0.2;

    [map setRegion:mapRegion animated: YES];

}

但是,每当调用此委托方法时,您就开始使用地图时,它将带我回到用户位置.我应该如何停止用户位置更新?香港专业教育学院试图把这段代码放在视图中确实加载,所以我得到了初始的缩放和居中,但这不起作用吗?或者,也许我可以将代码放入另一个map kit委托方法中,我只是不知道这样做的正确性.其他人怎么做到的?

But when you start playing with the map every time this delegate method gets called it brings me back to user location. How do i deal with this should i stop user location updates? Ive tried putting this code in view did load just so I get the initial zoom and center but it doesn't work? Or maybe i can put the code in another map kit delegate method i just don't know the proper one do it in. How does everyone else do this?

推荐答案

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{

是每次用户位置更改时都会调用的mapKit委托方法.这就是为什么您的地图将在每次通话时都以用户的位置为中心.启动应用程序时,您不能使用它来初始化用户位置上的地图.

Is a mapKit delegate method called each time the user's location has changed. Which is why your map will center on user's location on every call. You can't use that in order to init the map on user's position when app is launched.

我认为,如果无法在viewDidLoad上运行它,是因为当时应用尚不知道用户的位置.在此处放置一个断点,然后亲自看看.

I think if it's not working on viewDidLoad it's because at that time the user's location is not yet known by the app. Put a breakpoint there and see for yourself.

对我来说,您应该在viewDidLoad中添加一个观察者,当应用程序获取用户位置上的数据时会调用该观察者

For me you should add an observer in viewDidLoad that is called when the app gets the data on the user's location

// Check if user authorized use of location services
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
    // Add observer
    [self.mapView.userLocation addObserver:self
                                forKeyPath:@"location"
                                   options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
                                   context:NULL];
}

然后进入

- (void)observeValueForKeyPath:(NSString *)keyPath
                  ofObject:(id)object
                    change:(NSDictionary *)change
                   context:(void *)context;

等待keyPath位置"被调用. 首次触发时,将加载视图,并且应用程序知道用户的位置.然后,您可以将代码放置在地图上以用户位置为中心的位置. 但是,请确保删除观察者,以使它不会被多次调用.

Wait for the keyPath "location" to get called. When it's trigger for the first time, the view is loaded and the app knows the user's location. You can then put your code to center the map on user's position. But then make sure to remove the observer so it doesn't get call more than one time.

[self.mapView.userLocation removeObserver:self
                                   forKeyPath:@"location" context:NULL];

这篇关于用户位置Mapkit ViewDidLoad的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆