【问题标题】:Google Maps Android Set Visible Bounds谷歌地图安卓设置可见边界
【发布时间】:2015-07-13 10:31:57
【问题描述】:

首先,我对我的英语感到抱歉。 我尝试在 google maps android api 中设置精确的可见范围。例如地图只会显示美国的地区,用户不能滚动地图到加拿大。当您查看链接时,您可以理解我尝试做什么。我认为我可以使用 CameraChangeListener 并获取可见区域来做到这一点,但我无法实现算法。你能帮助我吗 ? http://sehirharitasi.ibb.gov.tr/

【问题讨论】:

    标签: android google-maps gis


    【解决方案1】:

    它不像你的链接那么简单和好,但它是可以工作的:

    //When you setup the map:
     map.setOnCameraChangeListener(new OnCameraChangeListener() {
         @Override
         public void onCameraChange(CameraPosition cameraPosition) {
             checkBounds();
         }
     });
    

    CheckBounds 函数可以检查可见区域是否在允许的范围内:

    public void checkBounds() {
      //non ricostruire allowedbounds ogni volta, sono sempre gli stessi, fatti il build nell'onCreate
      LatLngBounds actualVisibleBounds = map.getProjection().getVisibleRegion().latLngBounds;
      if (allowedBounds.contains(actualVisibleBounds)){
        return
      }else{
        map.animateCamera(CameraUpdateFactory.newLatLngBounds(allowedBounds));
      }
    }
    

    或者您可以使用视口的中心并检查是否在允许的区域内(以允许边界缩放)

    public void checkBounds() {
                    LatLngBounds.Builder builder = new LatLngBounds.Builder();
                    builder.include(northeast);
                    builder.include(southwest);
                    final LatLngBounds allowedBounds = builder.build();
    
                    LatLngBounds centro = map.getProjection().getVisibleRegion().latLngBounds;
    
                    if (allowedBounds.contains(centro.getCenter()))
                            return;
                    else {
                            map.animateCamera(CameraUpdateFactory.newLatLngZoom(defaultLatLng, zoomLevel));
                    }                
            }
    

    【讨论】:

    • 真的有效吗?因为onCameraChange() 将在地图滚动后被调用!
    • 是的,这就是 Google API 的行为,在 api 保持这些状态之前,没有比这更好的了!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-27
    • 1970-01-01
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多