使用 post 通知跟踪位置更新是否有效
Is it valid to use post notification to track location updates
我们可以使用 'post notification' 进行位置更新以在 google 地图上绘制当前位置吗?或者除此之外还有更好的实施方式吗?尽管我不希望在 googlemaps.
中将 KVO 用于 @"Mylocations"
在LocationTracker.m
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
for(int i=0;i<locations.count;i++)
{
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
[PlacesDetails sharedInstance].theLocation=theLocation;
if(newLocation != nil && (!(theLocation.latitude == 0.0 && theLocation.longitude == 0.0)))
{
self.myLastLocation = theLocation;
self.myLastLocationAccuracy= theAccuracy;
// Below implemented the post notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLocation" object:nil];
}
}
}
在ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateGMSCameraPostition) name:@"updateLocation"
object:nil];
}
-(void)updateGMSCameraPostition
{
NSLog(@"CALLED UPDATELOCATION OBSERVER");
mapView_.camera = [GMSCameraPosition cameraWithTarget:[PlacesDetails sharedInstance].theLocation
zoom:14];}
科班,
这主要取决于您如何设计您的应用程序。您可以为此使用不同的模式。
- NSNotificationCenter 也不错,但你需要在正确的时间移除观察者,如果你不这样做,那么多个通知可能会触发并导致你的应用出现可悲的行为。
- KVO,Willchangevalueforkey,你也可以用,但还是要小心使用。这个也很好衡量变化。
- 委托模式,你也可以为此使用委托模式。
- MVVM:- 如果您正在使用 MVVM(Modal View-View Modal)设计模式,那么我建议您使用块,这是我最喜欢的方式之一。
对于块,您可以查看这些链接。
我想这可能对你有帮助。
我们可以使用 'post notification' 进行位置更新以在 google 地图上绘制当前位置吗?或者除此之外还有更好的实施方式吗?尽管我不希望在 googlemaps.
中将 KVO 用于 @"Mylocations"在LocationTracker.m
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
for(int i=0;i<locations.count;i++)
{
CLLocation * newLocation = [locations objectAtIndex:i];
CLLocationCoordinate2D theLocation = newLocation.coordinate;
CLLocationAccuracy theAccuracy = newLocation.horizontalAccuracy;
[PlacesDetails sharedInstance].theLocation=theLocation;
if(newLocation != nil && (!(theLocation.latitude == 0.0 && theLocation.longitude == 0.0)))
{
self.myLastLocation = theLocation;
self.myLastLocationAccuracy= theAccuracy;
// Below implemented the post notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"updateLocation" object:nil];
}
}
}
在ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateGMSCameraPostition) name:@"updateLocation"
object:nil];
}
-(void)updateGMSCameraPostition
{
NSLog(@"CALLED UPDATELOCATION OBSERVER");
mapView_.camera = [GMSCameraPosition cameraWithTarget:[PlacesDetails sharedInstance].theLocation
zoom:14];}
科班, 这主要取决于您如何设计您的应用程序。您可以为此使用不同的模式。
- NSNotificationCenter 也不错,但你需要在正确的时间移除观察者,如果你不这样做,那么多个通知可能会触发并导致你的应用出现可悲的行为。
- KVO,Willchangevalueforkey,你也可以用,但还是要小心使用。这个也很好衡量变化。
- 委托模式,你也可以为此使用委托模式。
- MVVM:- 如果您正在使用 MVVM(Modal View-View Modal)设计模式,那么我建议您使用块,这是我最喜欢的方式之一。
对于块,您可以查看这些链接。
我想这可能对你有帮助。