Mohammed Hussain
Mohammed Hussain

Reputation: 176

mylocationButton Not updating My Location why?

As i had used my location button and this methods...

[mapView.settings setMyLocationButton:YES];

//AddObserverForDriverCurrentLocation
[mapView addObserver:self forKeyPath:@"myLocation" options:NSKeyValueObservingOptionNew context:NULL];

 [self.view addSubview:mapView];

dispatch_async(dispatch_get_main_queue(), ^{
    mapView.myLocationEnabled = YES;
});

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray<CLLocation *> *)locations 
{

NSLog(@"Locations:%@",[locations lastObject]);


}



- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

if([keyPath isEqualToString:@"myLocation"]) {
    CLLocation *location = [object myLocation];
    NSLog(@"Location, %@,", location);

    CLLocationCoordinate2D target =
    CLLocationCoordinate2DMake(22.7,75.8);

    [mapView animateToLocation:target];
    [mapView animateToZoom:17];
 }
}

But when I tap on myLocation button it is not uspdating my location please help

Upvotes: 1

Views: 156

Answers (1)

Anbu.Karthik
Anbu.Karthik

Reputation: 82779

try this

To move to your current position you have to:

mapView.myLocationEnabled = YES;

Then you can setup a key value observer in your viewWillAppear Method and then you get your location update with the Location Manager of the GoogleMaps SDK.

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Implement here to check if already KVO is implemented.
    ...
    [mapView addObserver:self forKeyPath:@"myLocation" options: NSKeyValueObservingOptionNew context: nil]
}

And then observe the property.

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"myLocation"] && [object isKindOfClass:[GMSMapView class]])
    {
        [mapView animateToCameraPosition:[GMSCameraPosition cameraWithLatitude: mapView.myLocation.coordinate.latitude
                                                                                 longitude: mapView.myLocation.coordinate.longitude
                                                                                      zoom: mapView.projection.zoom]]; // check that the zoom property once
    }
}

Do not forget to deregister your observer in the viewWillDisappear.

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    // Implement here if the view has registered KVO
    ...
    [mapView removeObserver:self forKeyPath:@"myLocation"];
}

Upvotes: 2

Related Questions