Raghul Sugathan
Raghul Sugathan

Reputation: 370

How to Move google Map along with periodically moving marker

I am working on a google map application, in which I have setup google map and a marker of a moving vehicle. I am getting the latitude and longitude from an API and I am updating the marker on google map periodically (Every 25 seconds).

         When updating marker I am unable to move the google map along with the marker. How to move the google map along with the periodically moving marker.

Any help should be a great help for me.

Upvotes: 0

Views: 1553

Answers (2)

Rybzor
Rybzor

Reputation: 183

So, in order to move the map along with marker location changes, you can do something like this:

public void onLocationChanged(Location loc) {
    //some marker movements here...

    CameraPosition currentPlace = new CameraPosition.Builder()
            .target(new LatLng(loc.getLatitude(), loc.getLongitude())
            .tilt(0f)
            .zoom(18)
            .build();

    map.animateCamera(CameraUpdateFactory.newCameraPosition(currentPlace), 200, null);
}

Of course you need access to GoogleMap object.

Upvotes: 3

G. Spyridakis
G. Spyridakis

Reputation: 441

The following code snippets illustrate some of the common ways to move the camera:

private static final LatLng SYDNEY = new LatLng(-33.88,151.21);
private static final LatLng MOUNTAIN_VIEW = new LatLng(37.4, -122.1);

private GoogleMap map;
... 

// Obtain the map from a MapFragment or MapView.

// Move the camera instantly to Sydney with a zoom of 15.
map.moveCamera(CameraUpdateFactory.newLatLngZoom(SYDNEY, 15));

// Zoom in, animating the camera.
map.animateCamera(CameraUpdateFactory.zoomIn());

// Zoom out to zoom level 10, animating with a duration of 2 seconds.
map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

// Construct a CameraPosition focusing on Mountain View and animate the camera to that position.
CameraPosition cameraPosition = new CameraPosition.Builder()
                .target(MOUNTAIN_VIEW)      // Sets the center of the map to Mountain View
                .zoom(17)                   // Sets the zoom
                .bearing(90)                // Sets the orientation of the camera to east
                .tilt(30)                   // Sets the tilt of the camera to 30 degrees
                .build();                   // Creates a CameraPosition from the builder

map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

Based on the Documentation for the Android Google Maps API

EDIT:

I found some interesting course on Udacity about Google Maps:

Upvotes: 1

Related Questions