iOS Google Map stop scrolling beyond radius

2020-05-09 17:40发布

How to stop scrolling beyond a radius. I need to restrict user to only scroll with in 20 meters of their current location.

I have tried below link:

Prevent scrolling outside map area on google map

But I am not able to find out the correct code to be put to implement current answer on this post.

1条回答
够拽才男人
2楼-- · 2020-05-09 18:00

You can use distanceFromLocation to calculate how far from your center coordinate point, then you can check if you scroll outside of the circle in the (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position delegate method.

Sample code:

  - (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];
    }
}
查看更多
登录 后发表回答