java 如何在谷歌地图 v2 中更正平滑移动标记?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29284218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to correct smooth moving the marker in google maps v2?
提问by Rexhaif
In my application needs to display a smooth "move" google maps marker from one point to another. I use the following method for the animation:
在我的应用程序中需要显示从一个点到另一个点的平滑“移动”谷歌地图标记。我使用以下方法制作动画:
public void animateMarker(final Marker marker, final LatLng toPosition,
final boolean hideMarker) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mMap.getProjection();
Point startPoint = proj.toScreenLocation(marker.getPosition());
final LatLng startLatLng = proj.fromScreenLocation(startPoint);
final long duration = 500;
final Interpolator interpolator = new LinearInterpolator();
handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);
double lng = t * toPosition.longitude + (1 - t)
* startLatLng.longitude;
double lat = t * toPosition.latitude + (1 - t)
* startLatLng.latitude;
marker.setPosition(new LatLng(lat, lng));
if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
But as a result of simply creating a new marker to the new location (though old is not removed):
但是作为简单地为新位置创建一个新标记的结果(虽然旧的没有被删除):
回答by kaho
I copied some of the code from the projectmentioned in the official video.
I tried to reproduce it with this code and this seems to be working for me, so hopefully my code would help you, even for a bit.
我试图用这段代码重现它,这似乎对我有用,所以希望我的代码能帮助你,即使是一点点。
static final LatLng SomePos = new LatLng(37.7796354, -122.4159606);
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
googleMap.setTrafficEnabled(false);
googleMap.setIndoorEnabled(false);
googleMap.setBuildingsEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(SomePos));
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
.target(googleMap.getCameraPosition().target)
.zoom(17)
.bearing(30)
.tilt(45)
.build()));
myMarker = googleMap.addMarker(new MarkerOptions()
.position(SomePos)
.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
.title("Hello world"));
googleMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener()
{
@Override
public boolean onMarkerClick(Marker arg0) {
final LatLng startPosition = myMarker.getPosition();
final LatLng finalPosition = new LatLng(37.7801569,-122.4148528);
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final Interpolator interpolator = new AccelerateDecelerateInterpolator();
final float durationInMs = 3000;
final boolean hideMarker = false;
handler.post(new Runnable() {
long elapsed;
float t;
float v;
@Override
public void run() {
// Calculate progress using interpolator
elapsed = SystemClock.uptimeMillis() - start;
t = elapsed / durationInMs;
v = interpolator.getInterpolation(t);
LatLng currentPosition = new LatLng(
startPosition.latitude*(1-t)+finalPosition.latitude*t,
startPosition.longitude*(1-t)+finalPosition.longitude*t);
myMarker.setPosition(currentPosition);
// Repeat till progress is complete.
if (t < 1) {
// Post again 16ms later.
handler.postDelayed(this, 16);
} else {
if (hideMarker) {
myMarker.setVisible(false);
} else {
myMarker.setVisible(true);
}
}
}
});
return true;
}
});
} catch (Exception e) {
e.printStackTrace();
}
回答by Fabián Bravo
I know this is an old question but...
我知道这是一个老问题,但是......
You can use value interpolator for this task :
您可以为此任务使用值插值器:
fun changePositionSmoothly(marker:Marker?, newLatLng: LatLng){
if(marker == null){
return;
}
val animation = ValueAnimator.ofFloat(0f, 100f)
var previousStep = 0f
val deltaLatitude = newLatLng.latitude - marker.position.latitude
val deltaLongitude = newLatLng.longitude - marker.position.longitude
animation.setDuration(1500)
animation.addUpdateListener { updatedAnimation ->
val deltaStep = updatedAnimation.getAnimatedValue() as Float - previousStep
previousStep = updatedAnimation.getAnimatedValue() as Float
marker.position = LatLng(marker.position.latitude + deltaLatitude * deltaStep * 1/100, marker.position.longitude + deltaStep * deltaLongitude * 1/100)
}
animation.start()
}
In Java
在 Java 中
void changePositionSmoothly(Marker marker, LatLng newLatLng) {
if (marker == null) {
return;
}
ValueAnimator animation = ValueAnimator.ofFloat(0f, 100f);
final float[] previousStep = {0f};
double deltaLatitude = newLatLng.latitude - marker.getPosition().latitude;
double deltaLongitude = newLatLng.longitude - marker.getPosition().latitude;
animation.setDuration(1500);
animation.addUpdateListener(animation1 -> {
float deltaStep = (Float) animation1.getAnimatedValue() - previousStep[0];
previousStep[0] = (Float) animation1.getAnimatedValue();
marker.setPosition(new LatLng(marker.getPosition().latitude + deltaLatitude * deltaStep * 1 / 100, marker.getPosition().latitude + deltaStep * deltaLongitude * 1 / 100));
});
animation.start();
}
To prevent error accumulation, at the end of the animation you can set the new position, but this is enough for most cases.
为防止错误累积,您可以在动画结束时设置新位置,但这对于大多数情况来说已经足够了。