3

如何在 google maps sdk for iOS 中设置带有位置的中心地图视图?使用 mapkit,我们可以做 setCenter:Location。如何使用适用于 iOS 的 google maps sdk 执行此操作。

4

3 回答 3

5

使用 GMSCameraPosition 构造函数创建一个新相机

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

然后使用方法

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

你也可以只使用

- (void)animateToLocation:(CLLocationCoordinate2D)location;

但是如果您想更好地控制相机的最终外观,则前者允许您在相机构造函数中更改缩放、方位和视角。

于 2013-02-22T15:29:48.847 回答
1

我使用这个辅助方法:

- (void)focusOnCoordinate:(CLLocationCoordinate2D) coordinate {
  [self.mapView animateToLocation:coordinate];
  [self.mapView animateToBearing:0];
  [self.mapView animateToViewingAngle:0];
  [self.mapView animateToZoom:ZOOM_LEVEL];
}
于 2013-02-22T18:32:42.553 回答
0

正如我在上面的评论中所述,“动画...”解决方案不提供真正的中心(而是提供顶部中心),而“moveCamera:”确实提供了真正的中心(但不提供动画)。

我唯一的解决方法是在调用“moveCamera:”之后添加对“animateToZoom:”的调用。这提供了真正的中心,并为中心添加了一些“幻觉”动画。

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

更新:更优雅的动画解决方案,缩小,然后放大和真正的中心(在标记点击上)。

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

注意:如果您想设置自己的坐标,只需将 marker.position 替换为您自己的 CLLocationCoordinate2D

于 2016-05-12T00:14:20.310 回答