David Papirov
David Papirov

Reputation: 121

How to move android google map camera with constant velocity and zoom

I cannot find the way to move android google map camera with constant velocity and zoom.

For example, if the current zoom is 15, and calling

CameraPosition pos= CameraPosition
                                .builder(map.mMap.getCameraPosition())
                                .target(target)
                                .zoom(15)
                                .build();

mMap.animateCamera(CameraUpdateFactory.newCameraPosition(pos), duration * 1000, null);

it modifies the zoom 15 - > 12 -> 15 (the mid value depends on the distance)

My target is to show an object moving on the map with constant velocity and always keep it in the center of the map

Tried to call moveCamera in the ValueAnimator of the object, but then the map jumps. I need to move it smoothly

Upvotes: 1

Views: 508

Answers (1)

Fazal Hussain
Fazal Hussain

Reputation: 1127

Use fused API for location update and move the camera to the updated location

mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(MainActivity.this, new OnSuccessListener<android.location.Location>() {
                @Override
                public void onSuccess(android.location.Location location) {
                    if(location!=null){
                        try{
                            LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

                            Geocoder geocoder = new Geocoder(MainActivity.this, Locale.getDefault());

                            String addresses = geocoder.getFromLocation(location.getLatitude(),
                                    location.getLongitude(), 1).get(0).getAddressLine(0);

                            marker_origin = new MarkerOptions().
                                    position(latLng).title(addresses)
                                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
                            mMap.addMarker(marker_origin);
                            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0f));


                            getPlacesData(latLng, "restaurant");
                        }catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            });

For further details check the android developer documentation

https://developer.android.com/training/location/receive-location-updates.html

Upvotes: 1

Related Questions