मैं जो करता हूं वह मुख्य स्थान से अपडेट प्रबंधित करने के लिए सिंगलटन क्लास को कार्यान्वित करता है। मेरे वर्तमान स्थान तक पहुंचने के लिए, मैं एक CLLocation *myLocation = [[LocationManager sharedInstance] currentLocation];
करते हैं आप मुख्य थ्रेड आप कुछ इस तरह कर सकता है ब्लॉक करने के लिए चाहता था:
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
}
हालांकि, है, क्योंकि यह पहले ही बताया गया है, मुख्य थ्रेड अवरुद्ध शायद नहीं है एक अच्छा विचार है, लेकिन यह एक अच्छा कूदने वाला बिंदु हो सकता है क्योंकि आप कुछ बना रहे हैं। आप यह भी देखेंगे कि मैंने जो कक्षा लिखा है वह स्थान अपडेट पर टाइमस्टैम्प की जांच करता है और पुराने स्थान से पुराने डेटा प्राप्त करने की समस्या को रोकने के लिए पुरानी किसी भी चीज़ को अनदेखा करता है।
यह मैंने लिखा सिंगलटन वर्ग है। कृपया ध्यान दें कि यह किनारों के चारों ओर थोड़ा मोटा है:
#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
स्रोत
2009-01-20 20:27:16
आप प्रश्न को स्पष्ट करना चाहिए: आप CLLocationManager उपयोग करने के लिए कैसे पता है - ऐसी "सुविधा विधि" को लागू करने में समस्या क्या है? – tcurdt
मुझे लगता है कि समस्या यह है कि यह इतना आसान नहीं है। आम तौर पर यह पहले एक पुराना स्थान देता है, उसके बाद एक जंगली रूप से निर्विवाद व्यक्ति होता है, जिसके बाद प्रगतिशील बेहतर होते हैं, आप किस बिंदु पर निर्णय लेते हैं कि आप चाहते हैं कि आप चाहते हैं? यह वास्तव में आप पर निर्भर है। – rustyshelf
अच्छा बिंदु, जंगली! –