Android 在位置更改时将标记设置为当前位置的动画
如何将标记移动到当前位置 我对两个固定地理位置已有经验 以下是我使用的:Android 在位置更改时将标记设置为当前位置的动画,android,google-maps,google-maps-markers,google-maps-android-api-2,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.permis
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(…)
显示当前位置
我已经通过了-
当前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秒移动一次。当您的位置发生变化时,无需删除标记并再次将其添加到地图上。您可以按如下方式设置标记的位置 首先,见: 如果位置更新间隔超过~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;
}
如果位置更新间隔太短(不要设置动画,只需移动标记):
您应该在
onMapReady
中初始化标记,而不是onLocationChanged
这是最终解决方案
我提供的用于获取、更新标记并将其动画化到当前位置的
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));
}
请随意使用此代码,如果您需要此代码的任何更新,请告诉我。
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));
}
}
“仅当我移动”是什么意思?是指光标移动时,还是单击其他地理位置时?@KingReload“仅当我移动”是指根据我当前的位置,它应该移动,就像我驾驶一辆汽车,从位置a开始,目标是到达位置B。。。所以我想让这个飞机标记器和我一起移动。。。现在清楚了吗?定期收听当前位置,并在位置发生变化时移动标记。我不明白你到底想做什么。@DorukhanArslan事实上你明白我的意思,我只是想知道当位置发生变化时,我如何动态移动标记?仍在从位置A静态移动到位置B。请检查我在上面发布的onLocationChanged(Location-Location)方法的代码我刚刚更新了您问题的标题。。。请接受这些更改。所以我希望你们能在这里得到更多的观点和答案,因为我想当前的标题会让人困惑。但我想等待一些其他的答案。我不知道动画片市场学做什么。为什么要在此处的每个位置添加新标记?如果更改onLocationChanged中onMapReady中已初始化标记的位置,会怎么样?有必要吗?我想它会在你的轨迹上创建多个标记。@DorukhanArslan你能告诉我最好的方法吗,你能分享我的两种方法的实际效果吗。。。onLocationCahanged和onMapReady。。。我想看到地图从一个位置移动到另一个基于当前位置…见我的答案上面。
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));
}
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));
}
}