2013-02-22 74 views

回答

5

与GMSCameraPosition构造函数创建一个新的相机

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

然后使用方法

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

您也可以只使用

- (void)animateToLocation:(CLLocationCoordinate2D)location; 

但前面的内容允许您更改相机构造函数中的缩放,方位和视角,以便更好地控制相机的最终外观。

+1

谢谢,但这种方法不把位置地图的中心。 – Mecid 2013-02-22 15:40:39

+0

赞同@Mecid这个解决方案顶部中心不只是中间居中:/ [mapView moveCamera:]提供了真正的中心,但并不动画 – greenhouse 2016-05-11 23:54:40

1

我用这个helper方法:

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

这个解决方案顶部居中不在中间:/ [mapView moveCamera:]提供真正的中心,但不动画 – greenhouse 2016-05-11 23:54:50

0

如上面我的意见,说“动画......”的解决方案没有提供真正的中心(提供顶级中锋代替),和“moveCamera:”确实确实提供真正的中心(但不生动)。

我唯一的工作就是在调用'moveCamera:'之后添加对'animateToZoom:'的调用。这提供了真正的中心,并添加了一些'虚幻的'动画中心。

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

更新:变焦出更优雅的动画解决方案,然后放大和真正的中锋(上标记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]; 
} 

注:如果你想设置自己的COORDS只是用自己的CLLocationCoordinate2D更换marker.position