繁体   English   中英

CameraPosition缩放和目标在Android 6中不起作用

[英]CameraPosition zoom and target doesn't work in Android 6

我正在做一个应用程序,该应用程序在读取带有此点的json后在地图上显示了一些POI。 我的应用程序在android 4.4上运行良好,但是当我在android 6(仿真器和真实设备)中运行时,相机无法按照我设置的水平缩放。 我没有收到有关任何不推荐使用的命令的错误或警告。

这是MainActivity中的代码

@Override
    public void onMapReady(GoogleMap map) {

        mapVar = map;
        // MapWrapperLayout initialization


        final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout) findViewById(R.id.map_relative_layout);
        // MapWrapperLayout initialization

        mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20));
        this.infoWindow = (ViewGroup) getLayoutInflater().inflate(R.layout.info_window, null);
        this.infoTitle = (TextView) infoWindow.findViewById(R.id.title);
        this.infoSnippet = (TextView) infoWindow.findViewById(R.id.snippet);
        this.infoButton = (Button) infoWindow.findViewById(R.id.button);

        // Setting custom OnTouchListener which deals with the pressed state
        // so it shows up
        this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
                getResources().getDrawable(R.drawable.round_but_green_sel), //btn_default_normal_holo_light
                getResources().getDrawable(R.drawable.round_but_red_sel)) //btn_default_pressed_holo_light
        {


            @Override
            protected void onClickConfirmed(View v, Marker marker) {


                // Here we can perform some action triggered after clicking the button
                //Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();


                Intent intent = new Intent(MainActivity.this, dettaglio.class);
                //EditText editText = (EditText) findViewById(R.id.edit_message);
                String titolo = marker.getTitle();
                String idPOI = allMarkersMapIds.get(marker);
                String IMGPOI = allMarkersMapImg.get(marker);
                String Desc = allMarkersMapDesc.get(marker);
                String  idUtentePOI = allMarkersMapidUtente.get(marker);
                String  idCategoria = allMarkersMapidCategoria.get(marker);

                LATLON = marker.getPosition();
                Bundle args = new Bundle();
                args.putParcelable("coordinatePOI", LATLON);
                intent.putExtra("bundle", args);

                intent.putExtra(EXTRA_MESSAGE, titolo);
                intent.putExtra(EXTRA_ID, idPOI);
                intent.putExtra(EXTRA_IMG, IMGPOI);
                intent.putExtra(EXTRA_Desc, Desc);
                intent.putExtra("IDCATEGORIAPOI", idCategoria);
                intent.putExtra("IDUTENTEPOI", idUtentePOI);


                startActivity(intent);

            }
        };
        this.infoButton.setOnTouchListener(infoButtonListener);


        map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // Setting up the infoWindow with current's marker info
                infoTitle.setText(marker.getTitle());
                infoSnippet.setText(marker.getSnippet());
                infoButtonListener.setMarker(marker);

                // We must call this to set the current marker and infoWindow references
                // to the MapWrapperLayout
                mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                return infoWindow;
            }
        });

/// HERE I CENTER AND ZOOM THE CAMERA
        CameraPosition googlePlex = CameraPosition.builder()
                .target(new LatLng(latitude, longitude))
                .zoom(15)
                .bearing(0)
                .tilt(45)
                .build();

        setUpMapIfNeeded(0, ricercaString);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newCameraPosition(googlePlex));
        map.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 2000, null);


    }

而且我在服务中做同样的事情,监听位置变化

@Override
public void onLocationChanged(Location location) {

    listPOI = MainActivity.getArrayList();
    // a bool value to understand if I can go ahead
    goAhead = MainActivity.getGoAhead();
    namesPOI = MainActivity.getNamePOI();
    range = PrefActivity.getRange();
    proximity = PrefActivity.getProximity();




    //checks whether the user wants to be notified
    if(proximity) {
    //check if the list of near POI is loaded
        if (goAhead) {

            if (utility.isNear(location, listPOI, namesPOI, range)) {
                POI = utility.getNamePOI();
                String msg = "You're near " + POI;
                tts1.tts.speak(msg, TextToSpeech.QUEUE_FLUSH, null);

            }

        }
    }
    getLocation();




    CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(location.getLatitude(),location.getLongitude()));
    MainActivity.mapVar.moveCamera(center);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15);
    MainActivity.mapVar.animateCamera(zoom);


}

但是变焦仍然处于世界水平...有人可以弄清楚为什么吗?

好的,我如下更改了onMapReady,现在它可以工作了。

@Override
    public void onMapReady(GoogleMap map) {

        mapVar = map;
        // MapWrapperLayout initialization


       CameraPosition googlePlex = CameraPosition.builder()
                .target(new LatLng(latitude, longitude))
                .zoom(15)
                .bearing(0)
                .tilt(45)
                .build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 2000, null);


        final MapWrapperLayout mapWrapperLayout = (MapWrapperLayout) findViewById(R.id.map_relative_layout);
        // MapWrapperLayout initialization

        mapWrapperLayout.init(map, getPixelsFromDp(this, 39 + 20));
        this.infoWindow = (ViewGroup) getLayoutInflater().inflate(R.layout.info_window, null);
        this.infoTitle = (TextView) infoWindow.findViewById(R.id.title);
        this.infoSnippet = (TextView) infoWindow.findViewById(R.id.snippet);
        this.infoButton = (Button) infoWindow.findViewById(R.id.button);

        // Setting custom OnTouchListener which deals with the pressed state
        // so it shows up
        this.infoButtonListener = new OnInfoWindowElemTouchListener(infoButton,
                getResources().getDrawable(R.drawable.round_but_green_sel), //btn_default_normal_holo_light
                getResources().getDrawable(R.drawable.round_but_red_sel)) //btn_default_pressed_holo_light
        {


            @Override
            protected void onClickConfirmed(View v, Marker marker) {


                // Here we can perform some action triggered after clicking the button
                //Toast.makeText(MainActivity.this, marker.getTitle() + "'s button clicked!", Toast.LENGTH_SHORT).show();


                Intent intent = new Intent(MainActivity.this, dettaglio.class);
                //EditText editText = (EditText) findViewById(R.id.edit_message);
                String titolo = marker.getTitle();
                String idPOI = allMarkersMapIds.get(marker);
                String IMGPOI = allMarkersMapImg.get(marker);
                String Desc = allMarkersMapDesc.get(marker);
                String  idUtentePOI = allMarkersMapidUtente.get(marker);
                String  idCategoria = allMarkersMapidCategoria.get(marker);

                LATLON = marker.getPosition();
                Bundle args = new Bundle();
                args.putParcelable("coordinatePOI", LATLON);
                intent.putExtra("bundle", args);

                intent.putExtra(EXTRA_MESSAGE, titolo);
                intent.putExtra(EXTRA_ID, idPOI);
                intent.putExtra(EXTRA_IMG, IMGPOI);
                intent.putExtra(EXTRA_Desc, Desc);
                intent.putExtra("IDCATEGORIAPOI", idCategoria);
                intent.putExtra("IDUTENTEPOI", idUtentePOI);


                startActivity(intent);

            }
        };
        this.infoButton.setOnTouchListener(infoButtonListener);


        map.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
            @Override
            public View getInfoWindow(Marker marker) {
                return null;
            }

            @Override
            public View getInfoContents(Marker marker) {
                // Setting up the infoWindow with current's marker info
                infoTitle.setText(marker.getTitle());
                infoSnippet.setText(marker.getSnippet());
                infoButtonListener.setMarker(marker);

                // We must call this to set the current marker and infoWindow references
                // to the MapWrapperLayout
                mapWrapperLayout.setMarkerWithInfoWindow(marker, infoWindow);
                return infoWindow;
            }
        });



        setUpMapIfNeeded(0, ricercaString);
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }

map.setMyLocationEnabled(true);

    }

1。

代替animateCamera > newCameraPosition使用moveCamera > newLatLngZoom

更换

    CameraPosition cameraPosition = new CameraPosition.Builder().target(location).zoom(12).build();
    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(location, 9));

  1. animateCamera使用CancelableCallback (可以解决的方法)

      googleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, 5), new GoogleMap.CancelableCallback() { @Override public void onFinish() { CameraUpdate zout = CameraUpdateFactory.zoomBy(-3f); googleMap.animateCamera(zout); } @Override public void onCancel() { } }); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM