1

我如何才能将标记当前位置动画标记当前位置上的位置改变

我已经有两个固定的地缘位置

这里经历是我用什么:

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

什么你的意思是“只有当我动”你的意思是当光标移动,还是点击其他地理位置? –

+0

@KingReload“只有当我移动时”意味着根据我现在的位置它应该移动,就像我驾驶一辆汽车并从位置A开始并且目标是到达位置B ...所以我希望这个飞机标记应该随着移动我...现在清楚了吗? – Sophie

+0

定期收听您的当前位置,并在您的位置发生变化时移动标记。我无法理解你到底想做什么。 –

回答

2

这是我所提供的final solution获取,更新和动画标记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)); 
} 

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

+0

我刚刚更新了您的问题的标题...请接受这些更改。所以我希望你能在这里看到更多的观点和答案,因为我猜想目前的标题混淆。 –

+0

但我想等一些其他答案 – Sophie

+0

我不确定animateMarkerToICS会做什么。为什么在这里的每个位置更新添加一个新标记?如果您更改onLocationChanged中已经初始化的marker-in-onMapReady的位置,该怎么办?是否有必要出于某种原因?我认为它会在你的轨迹上创建多个标记。 –

0

做一件事情把一个后台线程,每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); 
} 

Yo你应该在onMapReady而不是onLocationChanged初始化标记。