【发布时间】:2015-10-08 07:12:38
【问题描述】:
无论位置坐标如何,我都想将标记固定在地图的中心。如果用户在地图上移动相机,我希望它继续显示在中心,而标记中没有任何闪烁,并且该标记上显示新位置,如果可能,那么我该怎么做?请帮忙。谢谢
我正在使用 iOS google map sdk(objective-c)
【问题讨论】:
标签: ios objective-c iphone google-maps-sdk-ios
无论位置坐标如何,我都想将标记固定在地图的中心。如果用户在地图上移动相机,我希望它继续显示在中心,而标记中没有任何闪烁,并且该标记上显示新位置,如果可能,那么我该怎么做?请帮忙。谢谢
我正在使用 iOS google map sdk(objective-c)
【问题讨论】:
标签: ios objective-c iphone google-maps-sdk-ios
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];
然后等待地图:mapDidIdle
【讨论】: