本文介绍了限制将SKMapView拖动到某个城市的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将SKMapView拖动到某个城市.

I am trying to restrict dragging of SKMapView to a certain city.

这是我尝试过的内容,我已经阅读了Doc References和开放源代码.似乎SKBoundingBox可以做到这一点.

Here is what I have tried, I had gone through Doc References and open source code. Seems like SKBoundingBox does that.

但是这段代码不起作用.它仍然允许用户在任何地方平移.

But this piece of code doesn't work. It still allows user to pan everywhere.

SKBoundingBox *boundingBox = [SKBoundingBox boundingBoxWithTopLeftCoordinate:topLeftCoordinate bottomRightCoordinate:bottomRightCoordinate];

推荐答案

虽然对此没有集成支持,但是您可以采取解决方法.

While there is no integrated support for this, you can do a workaround.

应该替换公开的SDK演示应用程序,用于查看解决方法(MapDisplayViewController.m).

The file from here should replace the one from the Public SDK demo app for viewing the workaround (MapDisplayViewController.m).

这个想法是,您可以从我们提供的Maps.json中获取城市/国家/地区的边界框信息.然后,您需要实现以下回调:

The idea is that you can get the bounding box information of a city/country from the Maps.json provided by us. Then you need to implement the following callback:

- (void)mapView:(SKMapView *)mapView didChangeToRegion:(SKCoordinateRegion)region { 
    if (self.bbox) { 
        if ([self.bbox containsLocation:region.center]) { 
            self.previousRegion = region; 
        } else { 
            [self.mapView animateToLocation:self.previousRegion.center withDuration:0.2]; 
        } 
    } 
} 

此实现可确保当用户平移城市/国家/地区的边界框时,他将被移回到边界框所包含的先前区域.

This implementation ensures that when the user pans out of the bounding box of a city/country he will be moved back to the previous region contained by the bounding box.

缩放级别限制:可以通过SKMapsSettings的zoomLimits属性来限制缩放级别:

Zoom level restrictions: restricting the zoom level is possible through the zoomLimits property of the SKMapsSettings:

   SKMapZoomLimits zoomLimits; 
    zoomLimits.mapZoomLimitMin = 5.0f; 
    zoomLimits.mapZoomLimitMax = 14.0f; 
    self.mapView.settings.zoomLimits = zoomLimits;

这篇关于限制将SKMapView拖动到某个城市的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 00:27