动画标记到位置上的当前位置已更改

时间:2017-09-03 15:25:48

标签: android google-maps google-maps-markers google-maps-android-api-2

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

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

以下是我使用的内容:

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();

}

显示当前位置

我已经完成了 - https://stackoverflow.com/a/34582595/3585072

目前 onLocationChanged(位置位置)方法看起来像这样,我需要放在这里,根据位置更改动态移动我的标记:

@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));
}

4 个答案:

答案 0 :(得分:2)

这是我向获取,更新和动画标记提供给final solution的{​​{1}}

current location

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)); }

答案 1 :(得分:0)

做一件事就是把背景线程每隔2秒重复一次。删除上一个标记并在此线程内设置标记。这是我认为的一种可能方式。如果你移动经度,纬度也会改变,所以标记每2秒移动一次。

答案 2 :(得分:0)

当您的位置更改并在地图上再次添加标记时,无需删除标记。您可以按如下方式设置标记的位置。

首先,请参阅: 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初始化标记。

答案 3 :(得分:0)

我的MarkerAnimation版本中没有

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));
        }
}