Reputation: 58
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)
Upvotes: 1
Views: 5144
Reputation: 1252
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
Upvotes: 4