ios - 使用位置管理器
<p><p>我想做以下事情</p>
<ul>
<li>在 APP 委托(delegate)文件中的后台跟踪位置</li>
<li>在 ViewController 中跟踪某人的位置。</li>
</ul>
<p>这是我的做法:</p>
<p>在 APPDelegate 中:</p>
<p>在 applicationDidFinishLaunching 中:</p>
<pre><code>if ()
{
self.locationManager = [ init];
self.locationManager.delegate = self;
_startLocation = nil;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=500;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// Set Property
self.myLocation = newLocation;
BOOL isInBackground = NO;
if (.applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
// Handle location updates as normal, code omitted for brevity.
// The omitted code should determine whether to reject the location update for being too
// old, too close to the previous one, too inaccurate and so forth according to your own
// application design.
if (isInBackground)
{
;
}
if(newLocation.horizontalAccuracy <= 100.0f)
{
;
}
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
;
;
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(self.locationManager == nil)
{
self.locationManager = [ init];
}
;
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
self.locationManager = nil;
;
;
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
</code></pre>
<p>在 ViewController 中:</p>
<pre><code>- (void) startLocationChanges {
if ()
{
PLAppDelegate *appDelegate = (PLAppDelegate *) [ delegate];
if(appDelegate.locationManager == nil)
{
self.locationManager = [ init];
}
else {
self.locationManager = appDelegate.locationManager;
}
self.locationManager.delegate = self;
;
}
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
self.myLocation = newLocation;
;
if(newLocation.horizontalAccuracy <= 100.0f)
{
NSLog(@"stop updating location");
;
}
//_horizontalAccuracy.text = currentHorizontalAccuracy;
}
</code></pre>
<p>我这样做是否正确?当有人关闭应用时,位置服务似乎仍然处于开启状态。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>从设计的角度来看,我发现最好将位置管理器封装在单例类中(<a href="http://jinru.wordpress.com/2010/08/15/singletons-in-objective-c-an-example-of-cllocationmanager/" rel="noreferrer noopener nofollow">example</a>)。你在这里做什么:</p>
<pre><code>if ()
{
PLAppDelegate *appDelegate = (PLAppDelegate *) [ delegate];
if(appDelegate.locationManager == nil)
{
self.locationManager = [ init];
}
else {
self.locationManager = appDelegate.locationManager;
}
self.locationManager.delegate = self;
;
}
</code></pre>
<p>...非常困惑:您可能正在复制位置管理器对象或只是引用它(并且代码不清楚那里发生的事情而没有看到头文件)并且您通常不应该切换代表周围的位置经理。</p>
<p>使用单例类将为您提供一个处理与位置相关的事情的单一位置,并且您的 ViewController 和其他类可以随时请求这些数据。或者,您可以使用委托(delegate)模式在单例中存储对每个位置感知类(例如 ViewController 、应用程序委托(delegate))的引用,并在收到诸如 <code>locationManager:didUpdateToLocation 之类的回调时向这些类广播</code>.</p>
<p>我希望这会有所帮助 - 请查看我添加的链接以了解我的意思:单例模式是在这里使用的完美方式,因为您只希望在任何一个位置数据源都使用一个源时间。</p></p>
<p style="font-size: 20px;">关于ios - 使用位置管理器,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18337227/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18337227/
</a>
</p>
页:
[1]