fixed marker on center of google map ios sdk

2019-06-08 11:17发布

I want to fix the marker in the center of the map irrespective of the location coordinates. If the user moves the camera on map I want it to keep showing up in the center without any flickering in marker and new location on that marker shows,If it is possible then How can I do that? Please help out. Thanks

i am using iOS google map sdk(objective-c)

1条回答
趁早两清
2楼-- · 2019-06-08 12:00
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 ;)

    }

now the code above makes the marker stay as it was but it's flying while you drag the screen.

if you want it centered first the you have to set the marker coordinates to map.center -> coordinates conversion and then you have some animation to do:

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

then wait for map:mapDidIdle

查看更多
登录 后发表回答