如何停止滚动超出半径。我需要限制用户仅在20米的当前位置滚动。
我试过以下链接:
Prevent scrolling outside map area on google map
但是我无法找到正确的代码来实现这篇文章的当前答案。
答案 0 :(得分:1)
您可以使用distanceFromLocation计算距离中心坐标点的距离,然后可以检查是否在(void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position
委托方法中滚动到圆圈之外。
示例代码:
- (void)viewDidLoad {
[super viewDidLoad];
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
longitude:151.20
zoom:11];
mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
mapView.myLocationEnabled = YES;
mapView.settings.myLocationButton = YES;
self.view = mapView;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(-33.86, 151.20);
marker.title = @"Sydney";
marker.snippet = @"Australia";
marker.map = mapView;
CLLocationCoordinate2D circleCenter = CLLocationCoordinate2DMake(-33.86, 151.20);
GMSCircle *circ = [GMSCircle circleWithPosition:circleCenter
radius:10000];
circ.fillColor = [UIColor colorWithRed:0.25 green:0 blue:0 alpha:0.05];
circ.strokeColor = [UIColor redColor];
circ.strokeWidth = 5;
circ.map = mapView;
mapView.delegate = self;
}
- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(-33.86, 151.20);
int radius = 10000;
CLLocation *targetLoc = [[CLLocation alloc] initWithLatitude:position.target.latitude longitude:position.target.longitude];
CLLocation *centerLoc = [[CLLocation alloc] initWithLatitude:center.latitude longitude:center.longitude];
if ([targetLoc distanceFromLocation:centerLoc] > radius) {
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:center.latitude
longitude:center.longitude
zoom:mapView.camera.zoom];
[mapView animateToCameraPosition: camera];
}
}