用于更新多个视图的位置的单例 [英] singleton for location updating multiple views
问题描述
我希望在任何视图中按下导航栏中的locateButton后更新所有视图中的用户位置。我开始创建一个单身人士。
I want to update user location in ALL views, after the locateButton in the navbar is pressed in ANY view. I've started by creating a singleton.
Location.h
Location.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
@interface Location : NSObject <CLLocationManagerDelegate>
@property (nonatomic, strong) CLLocationManager* locationManager;
+ (Location*)sharedSingleton;
@end
Location.m
Location.m
#import "Location.h"
@implementation Location {
CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}
@synthesize locationManager;
- (id)init {
self = [super init];
if(self) {
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager setHeadingFilter:kCLHeadingFilterNone];
[self.locationManager startUpdatingLocation];
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
//do any more customization to your location manager
}
return self;
}
+ (Location*)sharedSingleton {
static Location* sharedSingleton;
if(!sharedSingleton) {
@synchronized(sharedSingleton) {
sharedSingleton = [Location new];
}
}
return sharedSingleton;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
latLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
longLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
addressLabel.text = [NSString stringWithFormat:@"%@, %@",
placemark.locality,
placemark.administrativeArea];
addressLabel.numberOfLines = 0;
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
@end
我想要在当前视图中按下顶部导航栏中的locationButton时,使用此选项更新用户lat和long。
I want to use this to update user lat and long on every view when the locationButton in the top navbar is pressed in the current view.
- (IBAction)locationPressed:(id)sender {
[[Location sharedSingleton].locationManager startUpdatingLocation];
}
NSNotifications会是最好的吗?如果是这样,我将如何在Location.m和视图控制器中实现它们?谢谢。
Would NSNotifications be best? If so how would I implement them in Location.m and the view controllers? Thanks.
推荐答案
我会做的是使用观察者模式。在你的单身人士中,保留所有观察者的 NSMutableArray
。
What I would do would be to use the observer pattern. In your singleton, keep an NSMutableArray
of all the observers.
NSMutableArray *observers;
您需要一个所有观察者都遵守的协议:
You'll need a protocol that all the observers will conform to:
@protocol LocationObserver <NSObject>
- (void)locationDidChange:(CLLocation *)updatedLocation;
@end
然后当位置发生变化时才这样做
and then when the location changes just do this
for (id<LocationObserver> observer in observers) {
[observer locationDidChange:newLocation];
}
你应该有一个addObserver和removeObserver方法,并且需要 id< LocationObserver>
并在数组中添加/删除它。
You should have an addObserver and removeObserver method as well that take an id<LocationObserver>
and add/remove it from the array.
这篇关于用于更新多个视图的位置的单例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!