1

Can we use the 'post notification' for location updates to plot current location on google maps?? Or any better way to implement other than this ? Although i wish not to use KVO for @"Mylocations" in googlemaps.

In 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];
    }
}
} 

In 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];}
Ketan Shinde
  • 1,847
  • 4
  • 18
  • 38
  • Its a legit way of passing data. Called the observer pattern if you want to look it up. – AMAN77 Aug 01 '16 at 08:28
  • @AMAN77 I have used the KVO pattern but crashing and console says " GMSMapView was deallocated while key value observers were still registered with it" I have implemented the same as referenced in link 'http://stackoverflow.com/questions/27193946/crash-while-getting-my-location-on-googlemaps-on-iphone' and tried every solution from there itself. So i decided not to use KVO. – Ketan Shinde Aug 01 '16 at 08:48

1 Answers1

1

Keban, That depends mainly how you have designed your app. You can use different pattern for this.

  1. NSNotificationCenter is also good, but you need to remove the observer on right time, if you don't do that then multiple notifications can fire and cause pathetic behaviour for your app.
  2. KVO, Willchangevalueforkey, you can also use that, but still you need to use that very carefully. This one is also good to measure the changes.
  3. Delegate Pattern, you can also use the delegate pattern for this.
  4. MVVM:- If you are using MVVM (Modal View-View Modal)designing pattern, then I would suggest you to go with blocks, this is one of my favourite way.

For blocks you can have a look over these links.

Blocks Apple Developer

Blocks RyPress

Inline Blocks

I think it might help you.

Sabby
  • 2,586
  • 2
  • 27
  • 41