如何在谷歌地图sdk中为iOS设置中心地图视图? 使用mapkit,我们可以做setCenter:Location。 如何使用谷歌地图为iOS发布sdk。
答案 0 :(得分:5)
使用GMSCameraPosition构造函数
创建一个新相机 + (GMSCameraPosition *)cameraWithTarget:(CLLocationCoordinate2D)target zoom:(CGFloat)zoom
然后使用方法
- (void)animateToCameraPosition:(GMSCameraPosition *)cameraPosition;
您也可以使用
- (void)animateToLocation:(CLLocationCoordinate2D)location;
但如果您想要更好地控制相机的最终外观,前一个版本允许您更改相机构造函数中的缩放,方位和视角。
答案 1 :(得分:1)
我使用这个辅助方法:
- (void)focusOnCoordinate:(CLLocationCoordinate2D) coordinate {
[self.mapView animateToLocation:coordinate];
[self.mapView animateToBearing:0];
[self.mapView animateToViewingAngle:0];
[self.mapView animateToZoom:ZOOM_LEVEL];
}
答案 2 :(得分:0)
如我在上面的评论中所述,'animate ...'解决方案不提供真正的中心(提供顶部中心),而'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];
}
注意:如果你想设置自己的coords,只需用你自己的CLLocationCoordinate2D替换marker.position