适用于iOS的Google Maps SDK上的平滑滚动

sha*_*her 12 google-maps objective-c ios

因此,我希望根据纬度和经度值将Google地图可滚动区域限制为地图上的某个矩形.为了做到这一点,我写了以下代码:

-(void) viewDidLoad{
    startLat = 43.331635f;
    startLong = -74.472913f;
    endLat = 43.329106f;
    endLong = -74.470589f;
    float cameraPosLat = (startLat + endLat) / 2.0f;
    float cameraPosLong = (startLong + endLong) / 2.0f;

    GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:cameraPosLat
                                                        longitude:cameraPosLong
                                                             zoom:18];
    mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
    mapView.mapType = kGMSTypeSatellite;
    mapView.delegate = self;
    mapView.myLocationEnabled = YES;
    [mapView setMinZoom:18 maxZoom:mapView.maxZoom];
    self.view = mapView;
    marker.map = mapView;
}

-(void) mapView:(GMSMapView *)delegateMapView didChangeCameraPosition:(GMSCameraPosition *)position{
    if(delegateMapView.camera.target.latitude > startLat){
        [delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(startLat, delegateMapView.camera.target.longitude)]];
    }
    if(delegateMapView.camera.target.latitude < endLat){
        [delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(endLat, delegateMapView.camera.target.longitude)]];
    }
    if(delegateMapView.camera.target.longitude < startLong){
        [delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(delegateMapView.camera.target.latitude, startLong)]];
    }
    if(delegateMapView.camera.target.longitude > endLong){
        [delegateMapView moveCamera:[GMSCameraUpdate setTarget:CLLocationCoordinate2DMake(delegateMapView.camera.target.latitude, endLong)]];
    }
}
Run Code Online (Sandbox Code Playgroud)

这很好用,它会停止我希望它停在的点上的地图,但是我注意到的一件事是,在可接受范围的边缘,滚动是非常跳跃的,而不是平滑的.我想知道是否有任何方法可以确保地图保持在指定的范围内,同时还保持边缘的平滑滚动.

非常感谢任何帮助,谢谢!

Dan*_*nil 1

试试这个:

-(void) mapView:(GMSMapView *)delegateMapView didChangeCameraPosition:(GMSCameraPosition *)position{
    if(delegateMapView.camera.target.latitude > startLat){
        [delegateMapView animateToCameraPosition:[GMSCameraPosition 
           cameraWithLatitude:startLat 
           longitude:delegateMapView.camera.target.longitude
           zoom:delegateMapView.camera.zoom]];
    }
    if(delegateMapView.camera.target.latitude < endLat){
        [delegateMapView animateToCameraPosition:[GMSCameraPosition 
           cameraWithLatitude:endLat 
           longitude:delegateMapView.camera.target.longitude
           zoom:delegateMapView.camera.zoom]];
    }
    if(delegateMapView.camera.target.longitude < startLong){
        [delegateMapView animateToCameraPosition:[GMSCameraPosition 
           cameraWithLatitude:delegateMapView.camera.target.latitude 
           longitude:startLong
           zoom:delegateMapView.camera.zoom]];
    }
    if(delegateMapView.camera.target.longitude > endLong){
        [delegateMapView animateToCameraPosition:[GMSCameraPosition 
           cameraWithLatitude:delegateMapView.camera.target.latitude 
           longitude:endLong
           zoom:delegateMapView.camera.zoom]];
    }
}
Run Code Online (Sandbox Code Playgroud)