在谷歌地图v2 android中移动标记
Posted
技术标签:
【中文标题】在谷歌地图v2 android中移动标记【英文标题】:Move markers in google map v2 android 【发布时间】:2012-11-23 13:30:17 【问题描述】:我正在 android Google maps v2 中进行地图聚类。 我只想将标记从一个地理点动画化到另一个地理点。有没有办法在 Google 地图 v2 中移动标记?
【问题讨论】:
【参考方案1】:在 google map v2 demo app 中有一个移动标记的示例 .. 在 play 库的示例中!!
我已经调查过了!!这里是移动标记的代码:-->
public void animateMarker(final Marker marker, final LatLng toPosition,
final boolean hideMarker)
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
Projection proj = mGoogleMapObject.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);
);
希望对大家有所帮助!!
【讨论】:
如果有人感兴趣,这在文件 adt-bundle-linux/sdk/extras/google/google_play_services/samples/maps/src/com/example/mapdemo/MarkerDemoActivity.java 中(在 4.2.2例子) @SandeepDhull 非常感谢。这非常有帮助。 final Interpolator interpolator = new LinearInterpolator();----- 给出错误 --- “不兼容的类型” ----- 如何解决? 嗨,当我们有多个点时,如何将标记从一个地理点动画到另一个地理点。 @SandeepDhull 不是关于时间,而是关于你编写的代码,它正在顺利地移动我的标记。【参考方案2】:使用 ValueAnimator 的正确方法:
double[] startValues = new double[]marker.getPosition().latitude, marker.getPosition().longitude;
double[] endValues = new double[]destLatLng.latitude, destLatLng.longitude;
ValueAnimator latLngAnimator = ValueAnimator.ofObject(new DoubleArrayEvaluator(), startValues, endValues);
latLngAnimator.setDuration(600);
latLngAnimator.setInterpolator(new DecelerateInterpolator());
latLngAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
@Override
public void onAnimationUpdate(ValueAnimator animation)
double[] animatedValue = (double[]) animation.getAnimatedValue();
marker.setPosition(new LatLng(animatedValue[0], animatedValue[1]));
);
latLngAnimator.start();
DoubleArrayEvaluator
在android.animation
包中不存在,有我的实现:
import android.animation.TypeEvaluator;
/**
* Inspired from @link android.animation.FloatArrayEvaluator
* <p/>
* This evaluator can be used to perform type interpolation between <code>double[]</code> values.
* Each index into the array is treated as a separate value to interpolate. For example,
* evaluating <code>100, 200</code> and <code>300, 400</code> will interpolate the value at
* the first index between 100 and 300 and the value at the second index value between 200 and 400.
*/
public class DoubleArrayEvaluator implements TypeEvaluator<double[]>
private double[] mArray;
/**
* Create a DoubleArrayEvaluator that does not reuse the animated value. Care must be taken
* when using this option because on every evaluation a new <code>double[]</code> will be
* allocated.
*
* @see #DoubleArrayEvaluator(double[])
*/
public DoubleArrayEvaluator()
/**
* Create a DoubleArrayEvaluator that reuses <code>reuseArray</code> for every evaluate() call.
* Caution must be taken to ensure that the value returned from
* @link android.animation.ValueAnimator#getAnimatedValue() is not cached, modified, or
* used across threads. The value will be modified on each <code>evaluate()</code> call.
*
* @param reuseArray The array to modify and return from <code>evaluate</code>.
*/
public DoubleArrayEvaluator(double[] reuseArray)
mArray = reuseArray;
/**
* Interpolates the value at each index by the fraction. If
* @link #DoubleArrayEvaluator(double[]) was used to construct this object,
* <code>reuseArray</code> will be returned, otherwise a new <code>double[]</code>
* will be returned.
*
* @param fraction The fraction from the starting to the ending values
* @param startValue The start value.
* @param endValue The end value.
* @return A <code>double[]</code> where each element is an interpolation between
* the same index in startValue and endValue.
*/
@Override
public double[] evaluate(float fraction, double[] startValue, double[] endValue)
double[] array = mArray;
if (array == null)
array = new double[startValue.length];
for (int i = 0; i < array.length; i++)
double start = startValue[i];
double end = endValue[i];
array[i] = start + (fraction * (end - start));
return array;
【讨论】:
兄弟,你拯救了我的一天。【参考方案3】:我是这样做的:
private Marker marker;
在 Google 地图上添加 标记时:
if(marker!=null)
marker.remove();
LatLng mLatLng = new LatLng(Your_Latitude, Your_Longitude);
marker = mMapHistory.addMarker(new MarkerOptions().position(mLatLng).title("My Title").snippet("My Snippet").icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher)));
希望这个最简单的方法能帮到你。
【讨论】:
这只会用新的标记更新标记的位置,但不会为地图上的标记设置动画。【参考方案4】:请试试这个:
private static void addMarkerWithCameraZooming(Context ctx, GoogleMap googleMap, double latitude, double longitude, String title, boolean dragabble)
LatLng current_latlng = new LatLng(latitude, longitude);
googleMap.addMarker(new MarkerOptions().position(current_latlng)
.title(title)
.snippet(getLocality(current_latlng, ctx))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.person_marker))
.draggable(dragabble)
);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).tilt(30).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
mGoogleMap.setOnMarkerDragListener(new OnMarkerDragListener()
@Override
public void onMarkerDragStart(Marker markerDragStart)
// TODO Auto-generated method stub
if (BuildConfig.DEBUG)
Log.i("Marker drag", "start");
@Override
public void onMarkerDragEnd(Marker markerDragEnd)
if (BuildConfig.DEBUG)
Log.i("Marker drag", "start");
@Override
public void onMarkerDrag(Marker markerDrag)
if (BuildConfig.DEBUG)
Log.i("Marker drag", "start");
);
【讨论】:
【参考方案5】:您好,我遇到了同样的问题,这里是我的帖子Google Maps Android api v2 and current location
你可以使用 Marker 的 remove() 方法用新的标记删除旧的标记。 我希望这是你需要的
【讨论】:
以上是关于在谷歌地图v2 android中移动标记的主要内容,如果未能解决你的问题,请参考以下文章
在谷歌地图 V2 android studio 中单击 InfoWindowClick 时,如何设置唯一键?