沿着谷歌地图android中的路径动画汽车(标记)

Posted

技术标签:

【中文标题】沿着谷歌地图android中的路径动画汽车(标记)【英文标题】:Animate a car(Marker) along a path in google map android 【发布时间】:2016-02-26 09:43:56 【问题描述】:

如果你有 List,我将分享一个沿路径移动标记的解决方案。位图将从使用 LatLng 列表沿路径移动。

【问题讨论】:

你的帖子应该是一个问题 用于动画折线路线 github.com/amalChandran/google-maps-route-animation 【参考方案1】:
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);
                
            
        
    );

【讨论】:

您能向我解释一下 List directionPoint 包含什么吗?谢谢 其从源到目的地的 LatLng 列表,其中汽车(标记)需要沿地图进行动画处理 我正在根据另一个应用程序的实时位置更新来制作汽车位置动画。那么当我只有当前位置时,如何获取 LatLng 列表呢?有没有办法可以延迟应用程序的绘制以收集一些更新,然后将所有更新添加到 List 对象并将其传递给您的方法并继续重复直到最终目的地? 嗨 Martain 我的要求是在地图上为汽车从源头到目的地设置动画,这不是实时的。在您的情况下,它是实时的,因此您不需要使用我的功能,而是根据当前位置更新标记并显示和隐藏标记以创建移动汽车的错觉 +1,谢谢。你的代码和这个要点有什么区别:gist.github.com/broady/6314689#file-1markeranimation-java【参考方案2】:

我也完成了同样的场景,这里给出了代码。

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;

希望这有帮助。

【讨论】:

以上是关于沿着谷歌地图android中的路径动画汽车(标记)的主要内容,如果未能解决你的问题,请参考以下文章

根据路径方向更改谷歌地图标记方向

在谷歌地图v2 android中移动标记

如何在android中根据谷歌地图不同的缩放级别调整谷歌地图标记的大小

动画相机以定位和设置谷歌地图中的平移

Android - 如何让谷歌地图显示一条动画连续闪烁点的折线

android谷歌地图json中的标记