在位置更改时将标记动画化到当前位置

Animate marker to current location on location changed

如何将标记移动到当前位置

我已经熟悉两个固定的地理位置

这是我使用的:

private void autoAnimateMarker() {

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    mMap.getUiSettings().setZoomControlsEnabled(true);

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    mMap.setMyLocationEnabled(true);

    LatLng fromLocation = new LatLng(-33.904438, 151.249852); 
    LatLng toLocation = new LatLng(-33.905823, 151.252422);
    Marker marker = mMap.addMarker(new MarkerOptions().position(fromLocation));
    MarkerAnimation.animateMarkerToICS(marker, toLocation, new LatLngInterpolator.Spherical());

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(fromLocation, 17F));
}

然后在onMapReady(...)

内调用
@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    autoAnimateMarker();

}

显示当前位置

我已经过了 -

目前 onLocationChanged(Location location) 方法如下所示,我需要放在这里,根据位置变化动态移动我的标记:

@Override
public void onLocationChanged(Location location)
{
    Toast.makeText(this, "Location Changed " + location.getLatitude()
            + location.getLongitude(), Toast.LENGTH_LONG).show();

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }

    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);

    //move map camera
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11));
}

做一件事放置一个每 2 秒重复一次的后台线程。删除以前的标记并在此线程内设置标记。这是我认为的一种可能方式。如果您移动经度和纬度也会发生变化,因此标记每 2 秒移动一次。

当您的位置发生变化时,无需删除标记并在地图上重新添加它。您可以按如下方式设置标记的位置。

首先,请看: https://gist.github.com/broady/6314689

如果位置更新间隔超过 ~3 秒:

public void onLocationChanged(Location location) {
    LatLng toLocation = new LatLng(location.getLatitude(), location.getLongitude());
    if (fromLocation != null) {
        mAnchorMarker.setPosition(fromLocation);
        MarkerAnimation.animateMarkerToICS(mAnchorMarker, toLocation, new LatLngInterpolator.Spherical());
    }
    fromLocation = toLocation;
}

如果位置更新间隔太短(不要动画,只移动标记):

public void onLocationChanged(Location location) {
    LatLng toLocation = new LatLng(location.getLatitude(), location.getLongitude());
    mAnchorMarker.setPosition(toLocation);
}

您应该在 onMapReady 而不是 onLocationChanged 中初始化标记。

这是我提供给 Get、Update 和 Animate Markercurrent location

final solution
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

if(ourGlobalMarker == null) { // First time adding marker to map
    ourGlobalMarker = mGoogleMap.addMarker(new MarkerOptions().position(latLng)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
    MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical());
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
} else {
    MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical());
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18));
}

Feel free to use this code and let me know if you need any update on this.

MarkerAnimation class 在我的 API 版本中不可用。以下是我在我的应用程序中的做法。

Marker mCurrLocationMarker = null;
LatLng latLng;

@Override
public void onLocationChanged(Location location){

    latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("My Location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));

    if(mCurrLocationMarker == null) { // Add marker and move camera on first time
        mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
    } else { // Update existing marker position and move camera if required
        mCurrLocationMarker.setPosition(latLng);
//        mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));
        }
}