Andrew Daw
Andrew Daw

Reputation: 29

GoogleMap.animateCamera not working on background thread

How do I change the camera focus inside a background thread? The zoom works fine in the thread, but changing the coordinates doesn't. Also changing the coordinates works if not used in the thread, but I intend on using delays so It needs to be in there.

@Override
        public void run() {

            if (points.size() > 0){
                Polyline line = map.addPolyline(new PolylineOptions()
                        .addAll(points)
                        .width(5)
                        .color(Color.RED));
                System.out.println(points.get(0).toString());
                map.animateCamera(CameraUpdateFactory.newLatLng(points.get(0)));
                map.animateCamera(CameraUpdateFactory.zoomTo(14));
            }
        }

Upvotes: 0

Views: 528

Answers (2)

Evgeniy Mishustin
Evgeniy Mishustin

Reputation: 3804

AnimateCamera API is implemented in another thread already for you. So you must call it in UI thread. Details about API

Upvotes: 0

Sreehari
Sreehari

Reputation: 5655

Probably because you may not be running this in a main UI thread

Change

map.animateCamera(CameraUpdateFactory.newLatLng(points.get(0)));
map.animateCamera(CameraUpdateFactory.zoomTo(14));

to

runOnUiThread(new Runnable() {

                     @Override
                     public void run() {
                            map.animateCamera(CameraUpdateFactory.newLatLng(points.get(0)));
                            map.animateCamera(CameraUpdateFactory.zoomTo(14));
                     }
                    });

Upvotes: 2

Related Questions