Google Maps SDK iOS - 防止地图在缩放时改变位置

Google Maps SDK iOS - prevent map from changing location on zoom

我有一段时间无法解决的问题。

我有一个 GMSMapView,中间有一个 imageView。结果 - 我可以拖动地图并始终将图钉居中。但是当我缩放地图时,问题就来了。 缩放时 - 地图目标的位置发生变化,我的 imageView 指向另一个位置。

我可以检测缩放是否发生变化,但实际上我无法强制 GMSMapView 仅进行缩放而不更改任何位置。

-(void) mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
{
    if (mZoomLevel != mapView.camera.zoom)
    {
        mZoomLevel = mapView.camera.zoom;
    }
}

所以基本上,即使我执行缩放,我也希望始终将图钉居中。 我尝试了 GMSMarker - 但它在跟随地图中心时出现性能问题。它不会立即执行,所以我决定使用 imageView。

主要问题:缩放时如何锁定地图当前位置?

嗯,这个问题折腾了10天,终于解决了! 答案很简单!

这里的几个步骤: 1. 在 GMSMapView 之上添加 imageView 作为标记 2. 将 UIPanGestureRecognizer 添加到 GMSMapView(不要忘记设置 delegate,这很重要) 3. 然后使用这个代码:

- (void) didPan:(UIPanGestureRecognizer*) gestureRecognizer
{
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded)
    {
        _mapView.settings.scrollGestures = true;
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if (gestureRecognizer.numberOfTouches > 1)
    {
        _mapView.settings.scrollGestures = false;
    }
    else
    {
        _mapView.settings.scrollGestures = true;
    }
    return true;
}

Google 使用 google maps sdk 1.10.0 修复了这个问题。

解决方案是在配置 GMSMapview 时简单地添加这一行:

_mapView.settings.allowScrollGesturesDuringRotateOrZoom = NO;

Swift 3:

mapView.settings.scrollGestures = false