iOS Google Map stop scrolling beyond radius

后端 未结 1 1177
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-29 10:41

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 scr

相关标签:
1条回答
  • 2021-01-29 11: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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题