Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Animate a car(Marker) along a path in google map android

I am going to share a solution of moving a marker along the path if you have List.The Bitmap will move along the path from using LatLng list.

like image 453
Nikhil Avatar asked Nov 24 '15 04:11

Nikhil


People also ask

How do I add a marker to Google Maps Android?

For adding a custom marker to Google Maps navigate to the app > res > drawable > Right-Click on it > New > Vector Assets and select the icon which we have to show on your Map. You can change the color according to our requirements. After creating this icon now we will move towards adding this marker to our Map.

How do I move a marker on Google Maps?

This is done by placing your cursor over the red marker, left click and hold the mouse button down. This captures the marker and allows you to move it to the right position on the map. Once you are happy with its new position release the left button.


2 Answers

public static void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint, final Bitmap bitmap) {


    Marker marker = myMap.addMarker(new MarkerOptions()
            .icon(BitmapDescriptorFactory.fromBitmap(bitmap))
            .position(directionPoint.get(0))
            .flat(true));

    myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10));

    animateMarker(myMap, marker, directionPoint, false);
}


private static void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint,
                                  final boolean hideMarker) {
    final Handler handler = new Handler();
    final long start = SystemClock.uptimeMillis();
    Projection proj = myMap.getProjection();
    final long duration = 30000;

    final Interpolator interpolator = new LinearInterpolator();

    handler.post(new Runnable() {
        int i = 0;

        @Override
        public void run() {
            long elapsed = SystemClock.uptimeMillis() - start;
            float t = interpolator.getInterpolation((float) elapsed
                    / duration);
            if (i < directionPoint.size())
                marker.setPosition(directionPoint.get(i));
            i++;


            if (t < 1.0) {
                // Post again 16ms later.
                handler.postDelayed(this, 16);
            } else {
                if (hideMarker) {
                    marker.setVisible(false);
                } else {
                    marker.setVisible(true);
                }
            }
        }
    });
}
like image 121
Nikhil Avatar answered Oct 08 '22 17:10

Nikhil


I'm also done with this same scenario, here given that code.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener {

private static Location oldLocation;
private GoogleMap mMap;
private LocationManager locationManager;
private Marker marker;
private static float angle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        Toast.makeText(this, "Location permissioin in not enable", Toast.LENGTH_SHORT).show();
        return;
    }
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2, 0, this);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
}

@Override
public void onLocationChanged(Location location) {
    locationUpdate(location);
    if (oldLocation != null) {
        double bearing = angleFromCoordinate(oldLocation.getLatitude(), oldLocation.getLongitude(), location.getLatitude(), location.getLongitude());
        changeMarkerPosition(bearing);
    }
    oldLocation = location;
}

private void locationUpdate(Location location) {
    LatLng latLng = new LatLng((location.getLatitude()), (location.getLongitude()));
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.car_right));
    mMap.clear();
    marker = mMap.addMarker(markerOptions);
    CameraPosition position = CameraPosition.builder()
            .target(new LatLng(location.getLatitude(), location.getLongitude()))
            .zoom(19)
            .tilt(30)
            .build();
    mMap.animateCamera(CameraUpdateFactory.newCameraPosition(position));
}

private void changeMarkerPosition(double position) {
    float direction = (float) position;
    Log.e("LocationBearing", "" + direction);

    if (direction==360.0){
        //default
        marker.setRotation(angle);
    }else {
        marker.setRotation(direction);
        angle=direction;
    }
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {

}

@Override
public void onProviderEnabled(String provider) {

}

@Override
public void onProviderDisabled(String provider) {

}


private double angleFromCoordinate(double lat1, double long1, double lat2,
                                   double long2) {
    double dLon = (long2 - long1);

    double y = Math.sin(dLon) * Math.cos(lat2);
    double x = Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
            * Math.cos(lat2) * Math.cos(dLon);

    double brng = Math.atan2(x, y);

    brng = Math.toDegrees(brng);
    brng = (brng + 360) % 360;
    brng = 360 - brng;
    return brng;
}}

hope this helpful.

like image 32
Gowsik Raja Avatar answered Oct 08 '22 18:10

Gowsik Raja