【问题标题】:Android Google Map move camera doesn't workAndroid谷歌地图移动相机不起作用
【发布时间】:2017-07-19 17:22:25
【问题描述】:

我已经查看了我可以找到的所有内容,但没有找到解决方案。我正在尝试做一些非常简单的事情:

protected void moveCamera(final LatLng position, final float zoom) {
    getMap(new OnMapReadyCallback() {
        @Override
        public void onMapReady(final GoogleMap googleMap) {

            logger.debug("Moving map to " + position);
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(position)
                    .zoom(zoom)
                    .build();
            googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

        }
    });
}

什么都没有发生。没有错误,没有相机移动。我也尝试过moveCamera。我尝试过对CameraUpdateFactory 的不同类型的调用,我尝试过分别移动和缩放,我尝试过延迟发布Handler。有时这很有效,但是如果我将片段与地图一起留下并回来,那么它就不起作用了。地图重置为纬度 0,0 和标准地图类型(它被设置为混合),我无法让它做任何事情。我知道该方法被记录语句调用。我需要做一些生命周期的事情来确保地图可以恢复吗?

我真的不想切换到 Mapbox 或其他东西,因为修复一个小错误需要做很多工作,但我需要它才能正常工作。

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    试试这个代码来解决这个相机问题。 您可以更改缩放级别、半径。

    int width = getResources().getDisplayMetrics().widthPixels;
        int height = getResources().getDisplayMetrics().heightPixels;
        height = Math.round(0.45f*height);
        int padding = (int) (width * 0.10);
    
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    LatLngBounds bounds = builder.include(new LatLng(INSERT_LAT,
                                INSERT_LNG)).build();
    CameraUpdate cameraUpdate = 
    CameraUpdateFactory.newLatLngBounds(bounds,width,height,padding);
        googleMap.moveCamera(cameraUpdate);
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(14), 1000, null);
    

    【讨论】:

      【解决方案2】:

      我已经使用了以下,它工作正常:

      GoogleMap mGoogleMap;
      
      LatLngBounds bounds = builder.build();
      int width = getResources().getDisplayMetrics().widthPixels;
      int height = getResources().getDisplayMetrics().heightPixels;
      height = Math.round(0.45f*height);
      int padding = (int) (width * 0.10);
      
      
      CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBounds(bounds,width,height,padding);
      
      // Add this where you want to move map camera
      mGoogleMap.moveCamera(CameraUpdateFactory.newLatLng("<pass_LatLng_here"));
      mGoogleMap.animateCamera(CameraUpdateFactory.zoomTo(14)); //set the zoom level here
      

      【讨论】:

      • 为什么要创建一个CameraUpdate 然后不使用它?
      • 如果我只使用moveCameraanimateCamera 部分,它什么也不做。 LatLngBounds bounds = LatLngBounds.builder().build()(我认为这是您想要的,因为您的代码 sn-p 中未定义 builder)会产生此错误:java.lang.IllegalStateException: no included points
      【解决方案3】:

      对我来说,Places Autocomplete api 中 onPlaceSelected 回调中的移动相机不起作用。

      我将 moveCamera 移到 runOnUiThread 中,现在它工作正常。

      【讨论】:

      • 原因是您试图从后台线程执行此操作,并且所有地图操作都必须在主/UI线程上。
      猜你喜欢
      • 2015-03-23
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2014-04-23
      相关资源
      最近更新 更多