【问题标题】:Is it valid to use post notification to track location updates使用发布通知来跟踪位置更新是否有效
【发布时间】:2016-08-01 16:46:55
【问题描述】:

我们可以使用“发布通知”进行位置更新以在谷歌地图上绘制当前位置吗?或者除此之外还有什么更好的实现方式?虽然我不想在 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];}

【问题讨论】:

  • 这是一种合法的数据传递方式。如果您想查找它,则称为观察者模式。
  • @AMAN77 我使用了 KVO 模式,但崩溃了,控制台说“GMSMapView 已解除分配,而键值观察者仍向其注册”我实现了与链接“stackoverflow.com/questions/27193946/…”中引用的相同,并且从那里尝试了所有解决方案。所以我决定不使用 KVO。

标签: ios objective-c cllocationmanager nsnotificationcenter gmsmapview


【解决方案1】:

科班, 这主要取决于您如何设计您的应用程序。您可以为此使用不同的模式。

  1. NSNotificationCenter 也不错,但您需要在正确的时间移除观察者,如果您不这样做,则可能会触发多个通知并导致您的应用出现可悲的行为。
  2. KVO,Willchangevalueforkey,你也可以使用它,但你仍然需要非常小心地使用它。这也是衡量变化的好方法。
  3. 委托模式,您也可以为此使用委托模式。
  4. MVVM:- 如果您使用 MVVM(Modal View-View Modal)设计模式,那么我建议您使用块,这是我最喜欢的方式之一。

对于区块,您可以查看这些链接。

Blocks Apple Developer

Blocks RyPress

Inline Blocks

我想这可能对你有帮助。

【讨论】:

  • 谢谢!!我对积木的掌握最少,我想去:)很快就会更新。
  • 欢迎@KetanShinde 干杯
猜你喜欢
  • 2011-12-11
  • 2016-03-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多