2013-02-22 97 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

相關問題