Google maps api 3 如何预防';GMSMapView';从无限水平滚动?

Google maps api 3 如何预防';GMSMapView';从无限水平滚动?,google-maps-api-3,ios7,google-maps-sdk-ios,gmsmapview,Google Maps Api 3,Ios7,Google Maps Sdk Ios,Gmsmapview,如何防止IOS 7上的“GMSMapView”无限水平滚动?我正在使用“MKTileOverlay”和“kGMSTypeNone”来显示一个遍及全世界的自定义图像 (当用户向左滚动图像时,图像会再次重复。我希望滚动到此为止。)嗯,我最初的想法是使用委托方法 -(void)地图视图:(GMSMapView*)地图视图将移动:(BOOL)手势; 在达到滚动限制时将相机向后移动。使用此选项,当用户滚动到末尾时,地图视图可能会临时超出限制(取决于委托方法调用的频率),但会将动画设置回所需的限制 如果您

如何防止IOS 7上的“GMSMapView”无限水平滚动?我正在使用“MKTileOverlay”和“kGMSTypeNone”来显示一个遍及全世界的自定义图像


(当用户向左滚动图像时,图像会再次重复。我希望滚动到此为止。)

嗯,我最初的想法是使用委托方法

-(void)地图视图:(GMSMapView*)地图视图将移动:(BOOL)手势;

在达到滚动限制时将相机向后移动。使用此选项,当用户滚动到末尾时,地图视图可能会临时超出限制(取决于委托方法调用的频率),但会将动画设置回所需的限制

如果您认为这种方法适合您,我们可以深入了解更多细节

更新:

好的,实际上我意识到你应该使用另一种委托方法:

- (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
取而代之的是,移动时,它会不断更新位置<代码>将移动在移动之前只调用一次。无论如何,我们的想法是设置地图视图和限制(边界框),即覆盖图的e/g框。我刚刚在我的位置周围创建了一个区域作为示例,并将相机的初始位置放置在其中。这是在
viewDidLoad
方法中设置的

此外,在
didChangeCameraPosition
方法中

  • 重新定位标记位置,以便可以看到当前指向的位置
  • 检查您是否已通过横向/纵向限制,这意味着您已通过覆盖的边界,并向后移动/设置动画
  • 请注意,下面的代码不会检查您是否通过了一个拐角(同时通过了横向和纵向限制),那么您可能最终会被禁止进入,但您可以使用更多的if轻松检查该条件

    具有mapview设置和委托方法的Viewcontroller如下所示,我已将完整的项目上载到此处:。请,不要忘记为您提供应用程序委托中的API密钥,因为我已从代码中删除了我的API密钥

    因此,视图控制器如下所示:

    #import "ViewController.h"
    #import <GoogleMaps/GoogleMaps.h>
    
    @interface ViewController () <GMSMapViewDelegate> {
        CLLocationCoordinate2D center, topLeft, topRight, bottomLeft, bottomRight;
        double leftLong, rightLong, bottomLat, topLat;
        GMSMarker *currentPosition;
    }
    
    @property (weak, nonatomic) IBOutlet GMSMapView *mapView;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        // GMSMapview itself is wired in storyboard, just set delegate
        self.mapView.delegate = self;
    
        // Lat/long limits (bounding box)
        leftLong = 15.0;
        rightLong = 16.0;
        bottomLat  = 45.0;
        topLat  = 46.0;
    
        // center coordinate for map view and set it to mapview
        center = CLLocationCoordinate2DMake(45.895064, 15.858220);
        GMSCameraPosition *cameraPosition = [GMSCameraPosition cameraWithLatitude:center.latitude
                                                                        longitude:center.longitude
                                                                             zoom:10];
        self.mapView.camera = cameraPosition;
    
        // Current position, for displaying marker
        currentPosition = [GMSMarker markerWithPosition:center];
        currentPosition.map = self.mapView;
    
        // coordinates based on coordinate limits for bounding box drawn as polyline
        topLeft     = CLLocationCoordinate2DMake(topLat, leftLong);
        topRight    = CLLocationCoordinate2DMake(topLat, rightLong);
        bottomLeft  = CLLocationCoordinate2DMake(bottomLat, leftLong);
        bottomRight = CLLocationCoordinate2DMake(bottomLat, rightLong);
    
    
        // Create visual bounding box with fat polyline
        GMSMutablePath *path = [[GMSMutablePath alloc] init];
        [path addCoordinate:topLeft];
        [path addCoordinate:topRight];
        [path addCoordinate:bottomRight];
        [path addCoordinate:bottomLeft];
        [path addCoordinate:topLeft];
    
        GMSPolyline *polyLine = [GMSPolyline polylineWithPath:path];
        polyLine.strokeWidth = 10.0;
        polyLine.map = self.mapView;
    
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    #pragma mark - Google Maps iOS SDK delegate methods
    
    - (void)mapView:(GMSMapView *)mapView didChangeCameraPosition:(GMSCameraPosition *)position {
    
    
        // Reposition GMSMarker introduced in viewDidLoad to updated position
        currentPosition.position = position.target;
    
        // The interesting part - a non-elegant way to detect which limit was passed
        // If each of lat/long limits is passed, map will move or animate to limiting position
    
        if (position.target.latitude > topLat) { // If you scroll past upper latitude
            // Create new campera position AT upper latitude and current longitude (and zoom)
            GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:topLat
                                                                          longitude:position.target.longitude
                                                                               zoom:position.zoom];
            // Now, you can go back without animation,
            //self.mapView.camera = goBackCamera;
    
            // or with animation, as you see fit.
            [self.mapView animateToCameraPosition:goBackCamera];
        }
    
        if (position.target.latitude < bottomLat) {
            GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:bottomLat
                                                                          longitude:position.target.longitude
                                                                               zoom:position.zoom];
            //self.mapView.camera = goBackCamera;
            [self.mapView animateToCameraPosition:goBackCamera];
        }
    
        if (position.target.longitude > rightLong) {
            GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:position.target.latitude
                                                                          longitude:rightLong
                                                                               zoom:position.zoom];
            //self.mapView.camera = goBackCamera;
            [self.mapView animateToCameraPosition:goBackCamera];
        }
    
        if (position.target.longitude < leftLong) {
            GMSCameraPosition *goBackCamera = [GMSCameraPosition cameraWithLatitude:position.target.latitude
                                                                          longitude:leftLong
                                                                               zoom:position.zoom];
            //self.mapView.camera = goBackCamera;
            [self.mapView animateToCameraPosition:goBackCamera];
        }
    
    
    }
    
    @end
    
    #导入“ViewController.h”
    #进口
    @界面视图控制器(){
    CLLOCATION坐标2D中心、左上角、右上角、左下角、右下角;
    双左长,右长,下宽,上宽;
    GMSMarker*当前位置;
    }
    @属性(弱、非原子)IBMOutlet GMSMapView*mapView;
    @结束
    @实现视图控制器
    -(无效)viewDidLoad
    {
    [超级视图下载];
    //GMSMapview本身连接在故事板中,只需设置委托
    self.mapView.delegate=self;
    //横向/纵向限制(边界框)
    leftLong=15.0;
    rightLong=16.0;
    bottomLat=45.0;
    托普拉特=46.0;
    //将地图视图的坐标居中,并将其设置为“地图视图”
    中心=CLLocationCoordinate2DMake(45.895064,15.858220);
    GMSCameraPosition*cameraPosition=[GMSCameraPosition cameraWithLatitude:center.latitude
    经度:中心经度
    缩放:10];
    self.mapView.camera=cameraPosition;
    //当前位置,用于显示标记
    currentPosition=[GMSMarker Marker with Position:center];
    currentPosition.map=self.mapView;
    //基于绘制为多段线的边界框的坐标限制的坐标
    topLeft=CLLocationCoordinate2DMake(topLat,leftLong);
    topRight=CLLocationCoordinate2DMake(topLat,rightLong);
    bottomLeft=CLLocationCoordinate2DMake(bottomLat,leftLong);
    bottomRight=CLLocationCoordination2dMake(底部横向,右侧纵向);
    //使用fat多段线创建可视边界框
    GMSMutablePath*path=[[GMSMutablePath alloc]init];
    [路径添加坐标:左上角];
    [路径添加坐标:右上角];
    [路径添加坐标:右下角];
    [路径添加坐标:左下角];
    [路径添加坐标:左上角];
    GMSPolyline*polyLine=[GMSPolyline polylineWithPath:path];
    折线.strokeWidth=10.0;
    polyLine.map=self.mapView;
    }
    -(无效)未收到记忆警告
    {
    [超级记忆警告];
    //处置所有可以重新创建的资源。
    }
    #pragma标记-谷歌地图iOS SDK委托方法
    -(无效)地图视图:(GMSMapView*)地图视图未更改相机位置:(GMSCameraPosition*)位置{
    //将viewDidLoad中引入的GMSMarker重新定位到更新的位置
    currentPosition.position=position.target;
    //有趣的部分-一种非优雅的方式来检测哪一个限制被通过
    //如果通过了每个横向/纵向限制,贴图将移动或设置动画到限制位置
    如果(position.target.latitude>topLat){//如果滚动到上纬度
    //在上纬度和当前经度创建新的campera位置(和缩放)
    GMSCameraPosition*goBackCamera=[GMSCameraPosition cameraWithLatitude:topLat
    经度:position.target.longitude
    缩放:位置。缩放];
    //现在,您可以不带动画返回,
    //self.mapView.camera=goBackCamera;
    //或者使用动画,如您所见。
    [self.mapView animateToCameraPosition:goBackCamera];
    }
    if(位置.目标.纬度<底部纬度){
    GMSCameraPosition*goBackCamera=[GMSCameraPosition cameraWithLatitude:bottomLat
    经度:position.target.longitude
    缩放:位置。缩放];
    //self.mapView.camera=goBackCamera;
    [self.mapView animateToCameraPosition:goBackCamera];
    }
    if(position.target.longitude>rightLong){
    GMSCameraPosition*goBackCamera=[GMSCameraPosition cameraWithLatitude:position.target.latitude
    长的
    
         func mapView(mapView: GMSMapView, didChangeCameraPosition position: GMSCameraPosition) {
    
            var latitude  = position.target.latitude;
            var longitude = position.target.longitude;
    
            if (position.target.latitude > bounds.northEast.latitude) {
                latitude = bounds.northEast.latitude;
            }
    
            if (position.target.latitude < bounds.southWest.latitude) {
                latitude = bounds.southWest.latitude;
            }
    
            if (position.target.longitude > bounds.northEast.longitude) {
                longitude = bounds.northEast.longitude;
            }
    
            if (position.target.longitude < bounds.southWest.longitude) {
                longitude = bounds.southWest.longitude;
            }
    
            if (latitude != position.target.latitude || longitude != position.target.longitude) {
    
                var l = CLLocationCoordinate2D();
                l.latitude  = latitude;
                l.longitude = longitude;
    
                mapView.animateToLocation(l);
    
            }
        }