【问题标题】:How to setCenter mapview with location in google maps sdk for iOS如何在适用于 iOS 的 google maps sdk 中使用位置设置中心地图视图
【发布时间】:2013-02-22 14:57:01
【问题描述】:

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

【问题讨论】:

    标签: google-maps-sdk-ios


    【解决方案1】:

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

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

    然后使用方法

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

    你也可以使用

    - (void)animateToLocation:(CLLocationCoordinate2D)location;
    

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

    【讨论】:

    • 谢谢,但这种方法不会将位置放在地图中心。
    • 同意@Mecid 这个解决方案顶部居中不只是居中居中:/ [mapView moveCamera:] 提供真正的居中,但没有动画
    【解决方案2】:

    我使用这个辅助方法:

    - (void)focusOnCoordinate:(CLLocationCoordinate2D) coordinate {
      [self.mapView animateToLocation:coordinate];
      [self.mapView animateToBearing:0];
      [self.mapView animateToViewingAngle:0];
      [self.mapView animateToZoom:ZOOM_LEVEL];
    }
    

    【讨论】:

    • 此解决方案顶部居中不只是居中居中:/ [mapView moveCamera:] 提供真正的居中,但不设置动画
    【解决方案3】:

    如上面我的 cmets 所述,“动画...”解决方案不提供真正的中心(而是提供顶部中心),而“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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-03
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多