固定标记在谷歌地图ios sdk的中心

时间:2015-10-08 07:12:38

标签: ios objective-c iphone google-maps-sdk-ios

我想将标记固定在地图的中心,而不管位置坐标如何。如果用户在地图上移动相机,我希望它继续显示在中心而没有任何闪烁的标记和该标记上的新位置显示,如果可能那么我该怎么做?请帮忙。感谢

我正在使用iOS谷歌地图sdk(objective-c)

1 个答案:

答案 0 :(得分:4)

GMSCameraPosition *lastCameraPosition;

- (void)mapView:(GMSMapView *)pMapView didChangeCameraPosition:(GMSCameraPosition *)position {

        /* move draggable pin */
        if (movingMarker) {

            // stick it on map and start dragging from there..
            if (lastCameraPosition == nil) lastCameraPosition = position;

            // Algebra :) substract coordinates with the difference of camera changes
            double lat = position.target.latitude - lastCameraPosition.target.latitude;
            double lng = position.target.longitude - lastCameraPosition.target.longitude;
            lastCameraPosition = position;
            CLLocationCoordinate2D newCoords = CLLocationCoordinate2DMake(movingMarker.googleMarker.position.latitude+lat,
                                                                          movingMarker.googleMarker.position.longitude+lng);
            [movingMarker.googleMarker setPosition:newCoords];
            return;
        }

    }

    - (void)mapView:(GMSMapView *)mapView idleAtCameraPosition:(GMSCameraPosition *)position {

        lastCameraPosition = nil; // reset pin moving, no ice skating pins ;)

    }

现在上面的代码使得标记保持不变,但是当你拖动屏幕时它会飞行。

如果你想让它居中,你必须将标记坐标设置为map.center - >坐标转换,然后你可以做一些动画:

CGPoint point = map.center; GMSCameraUpdate *camera =[GMSCameraUpdate setTarget:[map.projection coordinateForPoint:point]]; 
[map animateWithCameraUpdate:camera]; 

然后等待map:mapDidIdle