Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

moveCamera and animateCamera don't work the second time

I have the following method for updating my map:

private void setCamera() {
        if (currentLocation != null) {
            String[] coords = currentLocation.split(",", 2);
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(Double.parseDouble(coords[0]), Double.parseDouble(coords[1])));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
            mMap.moveCamera(center);
            mMap.animateCamera(zoom);
        }
    }

The first time I invoke this method immediately after opening the app, and this method is working fine. But after that I go to another fragment and then to the first fragment again. And in this case the method was invoked, currentLocation didn't equal null, center got the right LatLng object, but my map view didn't change and zoom is less than 5. What is wrong?

like image 694
Ksenia Avatar asked Jan 18 '16 11:01

Ksenia


1 Answers

Eventually I've resolved this issue. I changed the previous code as follows:

private void setCamera() {
        if (currentLocation != null) {
            String[] coords = currentLocation.split(",", 2);
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(Double.parseDouble(coords[0]), Double.parseDouble(coords[1])));
            CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
            mapFragment.getMap().moveCamera(center);
            mapFragment.getMap().animateCamera(zoom);
        }
    }

And now the map is displayed correctly.

like image 199
Ksenia Avatar answered Nov 11 '22 00:11

Ksenia