fixed marker on center of google map ios sdk

自古美人都是妖i 提交于 2019-12-08 04:24:22

问题


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:


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



来源:https://stackoverflow.com/questions/33009004/fixed-marker-on-center-of-google-map-ios-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!