3

How to setCenter mapview with location in google maps sdk for iOS? With mapkit we can do setCenter:Location. How do this with google maps sdk for iOS.

Mecid
  • 4,491
  • 6
  • 30
  • 30

3 Answers3

5

Create a new camera with the GMSCameraPosition constructor

+ (GMSCameraPosition *)cameraWithTarget:(CLLocationCoordinate2D)target zoom:(CGFloat)zoom

then use the method

- (void)animateToCameraPosition:(GMSCameraPosition *)cameraPosition;

You can also just use

- (void)animateToLocation:(CLLocationCoordinate2D)location;

but the previous allows you to change the zoom, bearing, and viewing angle within the camera constructor if you want more control over the final appearance of the camera.

  • 1
    Thanks, but this methods don't put location to center of map. – Mecid Feb 22 '13 at 15:40
  • agree with @Mecid this solution top centers not just center in the middle : / [mapView moveCamera:] provides true center, but doesn't animate – greenhouse May 11 '16 at 23:54
1

I use this helper method:

- (void)focusOnCoordinate:(CLLocationCoordinate2D) coordinate {
  [self.mapView animateToLocation:coordinate];
  [self.mapView animateToBearing:0];
  [self.mapView animateToViewingAngle:0];
  [self.mapView animateToZoom:ZOOM_LEVEL];
}
s12chung
  • 1,718
  • 1
  • 14
  • 29
  • 1
    this solution top centers not just center in the middle : / [mapView moveCamera:] provides true center, but doesn't animate – greenhouse May 11 '16 at 23:54
0

as stated in my comments above, the 'animate...' solutions do not provide true center (provides top center instead), and 'moveCamera:' does indeed provide true center (but doesn't animate).

my only work around was to add call to 'animateToZoom:' after call to 'moveCamera:'. this provides true center and adds some 'illusive' animation to center.

[mapView moveCamera:[GMSCameraUpdate setTarget:marker.position]];
[mapView animateToZoom:zoom_marker_select];

update: more elegant animation solution with zoom out, then zoom back in and true center (on marker tap).

#define zoom_marker_select 17.0f

// GMSMapViewDelegate callback
- (BOOL)mapView:(GMSMapView *)mview didTapMarker:(GMSMarker *)marker
{    
   [self animateToCenterMarker:marker zoom:zoom_marker_select];

   // NO = should continue with its default selection behavior (ie. display info window)
   return NO;
}

// custom true center with animation function
- (void)animateToCenterMarker:(GMSMarker*)marker zoom:(float)zoom
{
    /*
     NOTE: '[mapView animateToLocation:marker.position]' (or others like it below)
        does NOT provide true center, they provide top center instead
        only found 'moveCamera:' to provide true center (but animation doesn't occur)
        - workaround: add call to 'animateToZoom:' right after 'moveCamera:' call
        - elegant workaround: zoom out, center, then zoom in (animation sequnce below)
     */
    //[mapView animateToLocation:marker.position];
    //[mapView animateToCameraPosition:marker.position];
    //[mapView animateWithCameraUpdate:[GMSCameraUpdate setTarget:marker.position]];

   [CATransaction begin];
      [CATransaction setAnimationDuration:0.5f];
      [CATransaction setCompletionBlock:^{

          // 2) move camera to true center:
          //     - without animation
          //     - while zoomed out
          [mapView moveCamera:[GMSCameraUpdate setTarget:marker.position]];

          [CATransaction begin];
              [CATransaction setAnimationDuration:0.5f];

              // 3) animate dur 0.5f:
              //     - zoom back in to desired level
              [mapView animateToZoom:zoom];
          [CATransaction commit];
      }];

      // 1) animate dur 0.5f:
      //     - zoom out to current zoom - 1
      //     - set location to top center of selected marker
      //     - set bearing to true north (if desired)
      float zoomout = mapView.camera.zoom -1;
      [mapView animateToZoom:zoomout];
      [mapView animateToLocation:marker.position];
      [mapView animateToBearing:0];
   [CATransaction commit];
}

note: if you want to set your own coords just replace marker.position with your own CLLocationCoordinate2D

greenhouse
  • 1,231
  • 14
  • 19