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

时间:2016-12-02 12:54:16

标签: java android android-6.0-marshmallow

我正在使用此点读取json后,在地图上显示一些POI的应用程序。 我的应用程序在android 4.4中运行良好,但是当我在android 6(仿真器和真实设备)中运行它时,相机不会在I&#m; m设置的水平上进行缩放。 我没有收到任何关于某些弃用命令的错误或警告。

这是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);


}

但变焦仍处于世界水平......有人可以找出原因吗?

2 个答案:

答案 0 :(得分:0)

好的,我按如下方式更改了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 :(得分:0)

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. CancelableCallback中使用animateCamera(解决方法)

        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() {
    
            }
        });