Анимация автомобиля (маркер) по пути в google map android
Я собираюсь поделиться решением перемещения маркера по пути, если у вас есть список.Растровое изображение будет перемещаться по пути из списка LatLng.
2 ответов
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);
}
}
}
});
}
Я также закончил с этим же сценарием, здесь, учитывая этот код.
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;
}}
надеюсь, что это полезно.