Jeff Bootsholz
Jeff Bootsholz

Reputation: 3068

ios Google Map - how to get my device location without zooming to the camera

I am working on the button to get my location regularly. When it comes to the implementation, the device will let Google Map camera being center of the map regularly even without using GMSCameraUpdate to focus. Would you please tell me what to enhance so that we can regularly get the updated device location without camera zooming?

The below is my working on triggering the zooming after onClick

 if(self.myMapView.myLocation !=nil){
        [self.locationManager stopUpdatingLocation];
        [CATransaction begin];
        [CATransaction setAnimationDuration:0.2];
        GMSCameraUpdate *move = [GMSCameraUpdate setTarget:self.myMapView.myLocation.coordinate  zoom:self.myMapView.camera.zoom];
        [self.myMapView animateWithCameraUpdate:move];
        [CATransaction commit];
    }

Delegate after getting location

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations{
    CLLocation *location = [locations lastObject];


    if(location==nil){
        location = self.myMapView.myLocation;
    }

    myDeviceLocation = location;

    NSLog(@"adasdads zoom ");
    if (markera == nil) {
        markera = [[GMSMarker alloc] init] ;
        markera.position   = CLLocationCoordinate2DMake(22.2855200, 114.1576900);
        markera.groundAnchor = CGPointMake(0.5f, 0.97f); // Taking into account walker's shadow

        markera.map = self.myMapView;


    }else {
        [CATransaction begin];
        [CATransaction setAnimationDuration:2.0];
        markera.position = location.coordinate;
        markera.icon = nil;
        [CATransaction commit];

    }

    GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
    [self.myMapView animateWithCameraUpdate:move];
}



-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)statusX {

    NSLog(@"status  : %d" , statusX);
    if (status == kCLAuthorizationStatusDenied) {
        //location denied, handle accordingly
        NSLog(@"denied ");

    }
else if (statusX == kCLAuthorizationStatusAuthorized) {
    //hooray! begin startTracking
    NSLog(@"proceed ");
    dispatch_async(dispatch_get_main_queue(), ^{
        self.myMapView.myLocationEnabled = YES;
    });



}else   if (statusX == kCLAuthorizationStatusAuthorizedAlways || statusX == kCLAuthorizationStatusAuthorizedWhenInUse) {
    self.myMapView.myLocationEnabled = YES;

    NSLog(@"gogogo ");
    dispatch_async(dispatch_get_main_queue(), ^{
      //  self.myMapView.myLocationEnabled = YES;
    });

Upvotes: 0

Views: 218

Answers (1)

rmp
rmp

Reputation: 3513

You can try this

Remove this code from your didUpdateLocations delegate:

GMSCameraUpdate *move = [GMSCameraUpdate setTarget:location.coordinate zoom:17];
[self.myMapView animateWithCameraUpdate:move];

Move it to a new function that you can call later.

In didUpdateLocations set your new user location and then call [locationManager stopUpdatingLocation] and then call your new custom function to set the camera.

In your new custom function, change:

[self.myMapView animateWithCameraUpdate:move];

To:

CLLocationCoordinate2D target = myLocation;  //2d coord
self.myMapView.camera = [GMSCameraPosition cameraWithTarget:target zoom:17];

Upvotes: 1

Related Questions