如何用给定的用户位置初始化MKMapView? [英] HowTo initialise MKMapView with a given user location?

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

问题描述

我的应用程式知道目前的使用者位置(CoreLocation.framework)。
一旦用户打开一个新的MapView,他的iPhone开始再次搜索当前位置。
是否可以跳过或更改mkMapView的第一个用户位置?

My app knows the current user position (CoreLocation.framework). As soon as the user opens a new MapView his iPhone starts searching for the current position again. Is it possible to skip that or to change the first user position for mkMapView?

编辑:

是否可以覆盖MKMapView并使用其他LocationManager?

Is it possible to overwrite MKMapView and use an other LocationManager?

推荐答案

是的,可以有一个单独的位置管理器对象,并将其值分配给mapview(BTW,我使用'='作为列表前缀,以防止SO代码格式器从borking)。

Yes, it is possible to have a separate location manager object and assign its value to the mapview (BTW, I'm using '=' below as list prefix to prevent the SO code-formatter from borking).

= In您的 UIViewController 维护两个单独的属性:一个到 MKMapView 和一个到 CLLocationManager

= In your UIViewController maintain two separate properties: one to a MKMapView and one to a CLLocationManager.

=使用 MKMapView 和任何其他窗口chrome创建一个XIB文件。将插座连接到控制器。确保 MKMapView 不会跟随用户位置。

= Create a XIB file with the MKMapView and any other window chrome you want. Connect the outlets to the controller propeties. Make sure MKMapView does NOT follow user location.

=让UIViewController实现 CLLocationManagerDelegate 协议 - 特别是 locationManager:didUpdateToLocation:fromLocation:方法,只要有新的位置值可用。我们将控制器设置为位置管理器的委托。

= Have the UIViewController implement the CLLocationManagerDelegate protocol--especially the locationManager:didUpdateToLocation:fromLocation: method which will be called whenever a new location value is available. We'll be setting the controller as the delegate for the location manager.

=在viewController的 loadView 使用 MKMapView 加载NIB。为了给用户反馈,你可能需要放置一个 UIActivityIndi​​catorView 微调器,并将其设置为 startAnimating 。然后你开始:

= In the viewController's loadView method, load the NIB with the MKMapView in it. To give user feedback you may want to put up a UIActivityIndicatorView spinner and set it to startAnimating. Then you start with:

self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
self.locationManager.distanceFilter = 10; // or whatever
[self.locationManager startUpdatingLocation];

=在 locationManager:didUpdateToLocation:fromLocation:检查事件是否自最近N秒后更新。然后告诉位置管理器停止更新,微调器停止动画,并获取纬度/经度数据,并将其与视图跨度和区域一起分配给地图视图,以便缩放并居中到正确的位置。

= In locationManager:didUpdateToLocation:fromLocation: check to see if the event was updated since the last N seconds. Then tell the location manager to stop updating, the spinner to stop animating, and get the lat/long data and assign it to the map view along with a view span and region so it zooms and centers to the right place.

=现在这里是棘手的部分:蓝色大理石'throbber'是mapview跟踪用户位置的一个功能。你必须暂时伪造,直到真正的人踢(或只是使用一个单独的标记为当前位置,并保持自己的位置)。我个人会使用用户熟悉的蓝色大理石。

= Now here's the tricky part: the blue marble 'throbber' is a feature of the mapview tracking user location. You'll have to momentarily 'fake it' until the real one kicks in (or just use a separate marker for the current location and maintain its position yourself). Personally I'd go with the blue marble that the user is familiar with.

=为了使它在启动时显示,您需要创建一个自定义MKAnnotationView,只是在位置管理器返回的位置添加蓝色大理石图形。这意味着拍摄位置显示的Mapview的快照,然后只拍摄蓝色大理石,并将其用作自定义注释视图的图像。

= To make it so it shows right at startup you will need to create a custom MKAnnotationView with just the blue marble graphic added at the location returned by the location manager. This means taking a snapshot of a Mapview with the location showing, then photoshopping just the blue marble out and using it as the image for a custom annotation view.

=如果您想要它主动跟随地图,你可以启用mapview的userlocation跟踪,当它获取实际的数据,你隐藏你先前设置的标记,并让mapview做更新。另一个选项是允许现有的位置管理器每秒左右继续接收更新,并自己更新蓝色大理石注释的位置。

= If you want it to actively follow the map, you can enable the userlocation tracking of the mapview and when it gets the actual data, you hide your previously set marker and let the mapview do the updating. The other option is to allow the existing location manager to continue receiving updates every second or so and update the position of the blue marble annotation yourself.

=让mapview自己userLocation做更新添加到viewDidLoad:

= To let the mapview's own userLocation do the updating add to viewDidLoad:

[self.map.userLocation addObserver:self 
forKeyPath:@"location" 
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) 
context:NULL];
self.map.showsUserLocation = YES; // starts updating user location

=实现 observeValueForKeyPath 。当mapview的 userlocation location 属性具有值时,它会被调用:

= Implement observeValueForKeyPath. It gets called when the location attribute of the mapview's userlocation has a value:

-(void)observeValueForKeyPath:(NSString *)keyPath 
      ofObject:(id)object 
        change:(NSDictionary *)change 
       context:(void *)context 
{
     if ([self.map isUserLocationVisible]) {
         [self.locationManager stopUpdatingLocation];
         self.ownBlueMarble.hidden = YES;
     }
     // The current location is in self.map.userLocation.coordinate
}

=为了避免显示当前位置的预热延迟,保持对包含地图和位置管理器的viewController的引用,以便它不会消失(这是一个内存hog,但if你释放它,你必须再次等待,直到MapView加载图块,并准备好去)。

= To avoid the warm-up delay in showing current location, keep a reference to the viewController containing the map and the location manager so it doesn't go away (it's a bit of a memory hog but if you release it you'll have to wait again until MapView loads the tiles and is ready to go).

=在 viewWillLoad 中,您可以将最后一个已知位置填充到自定义Bluemarble注释中并显示。打开/关闭userLocation跟踪,当你收到通知相同的hide-the-annotation-show-the-real-marble技巧将工作。

= In viewWillLoad you can stuff the last known location into the custom bluemarble annotation and show it. Toggle on/off the userLocation tracking and when you get the notification the same hide-the-annotation-show-the-real-marble trick will work. The mapview's own location manager kicks in and when it has the data, you can make your annotation marker disappear.

=您可能希望实现viewController的 viewWillDisappear 方法,并手动关闭在地图视图上的 userLocation 跟踪,因此下次启动视图时默认关闭。你还需要得到最后一个已知的 userLocation 并保存下一个get-go。这样,你可以在 viewWillAppear 方法中执行所有定位标记,而不必担心userLocation干扰,直到你准备好。

= You might want to implement the viewController's viewWillDisappear method and manually turn off userLocation tracking on the mapview so it's off by default the next time the view is brought up. You'll also want to get the last known userLocation and save it for the next get-go. That way, you can do all the positioning marker-juggling in the viewWillAppear method and not have to worry about userLocation interfering until you're ready for it.

祝你好运。

这篇关于如何用给定的用户位置初始化MKMapView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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