获取iPhone当前位置的最简单方法是什么?

编程入门 行业动态 更新时间:2024-10-08 04:35:06
本文介绍了获取iPhone当前位置的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我已经知道如何使用CLLocationManager,所以我可以用代表和所有这些来完成它。

I already know how to use the CLLocationManager, so I could do it the hard way, with delegates and all that.

但是我希望有一个方便的方法,只需获取当前位置一次,然后阻塞直到得到结果。

But I'd like to have a convenience method that just gets the current location, once, and blocks until it gets the result.

推荐答案

我所做的是实现一个单例类来管理来自核心位置的更新。要访问我当前的位置,我执行 CLLocation * myLocation = [[LocationManager sharedInstance] currentLocation]; 如果要阻止主线程,可以执行以下操作:

What I do is implement a singleton class to manage updates from core location. To access my current location, I do a CLLocation *myLocation = [[LocationManager sharedInstance] currentLocation]; If you wanted to block the main thread you could do something like this:

while ([[LocationManager sharedInstance] locationKnown] == NO){ //blocking here //do stuff here, dont forget to have some kind of timeout to get out of this blocked //state }

然而,正如已经指出的那样,阻止主线程可能不是一个好主意,但是当你正在构建一些东西时,这可能是一个很好的跳跃点。您还会注意到我编写的类检查位置更新的时间戳并忽略任何旧的,以防止从核心位置获取过时数据的问题。

However, as it has been already pointed out, blocking the main thread is probably not a good idea, but this can be a good jumping off point as you are building something. You will also notice that the class I wrote checks the timestamp on location updates and ignores any that are old, to prevent the problem of getting stale data from core location.

这是我写的单身课程。请注意,边缘有点粗糙:

This is the singleton class I wrote. Please note that it is a little rough around the edges:

#import <CoreLocation/CoreLocation.h> #import <Foundation/Foundation.h> @interface LocationController : NSObject <CLLocationManagerDelegate> { CLLocationManager *locationManager; CLLocation *currentLocation; } + (LocationController *)sharedInstance; -(void) start; -(void) stop; -(BOOL) locationKnown; @property (nonatomic, retain) CLLocation *currentLocation; @end @implementation LocationController @synthesize currentLocation; static LocationController *sharedInstance; + (LocationController *)sharedInstance { @synchronized(self) { if (!sharedInstance) sharedInstance=[[LocationController alloc] init]; } return sharedInstance; } +(id)alloc { @synchronized(self) { NSAssert(sharedInstance == nil, @"Attempted to allocate a second instance of a singleton LocationController."); sharedInstance = [super alloc]; } return sharedInstance; } -(id) init { if (self = [super init]) { self.currentLocation = [[CLLocation alloc] init]; locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; [self start]; } return self; } -(void) start { [locationManager startUpdatingLocation]; } -(void) stop { [locationManager stopUpdatingLocation]; } -(BOOL) locationKnown { if (round(currentLocation.speed) == -1) return NO; else return YES; } - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120) { self.currentLocation = newLocation; } } - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } -(void) dealloc { [locationManager release]; [currentLocation release]; [super dealloc]; } @end

更多推荐

获取iPhone当前位置的最简单方法是什么?

本文发布于:2023-10-19 17:37:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:当前位置   最简单   方法   iPhone

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!