Google地图:移动标记和地图一起与用户一起顺利吗? [英] Google map: moving marker and map together smoothly along with the user?
问题描述
我必须在Google地图上显示实时/实时用户的移动位置。下面的方法来动画标记。
private 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);
最终持续时间= 1000;
final插值器插值器=新的LinearInterpolator();
handler.post(new Runnable(){
@Override
public void run(){
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float)逝去的
/持续时间);
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){
// 16ms后再次发布。
handler.postDelayed(this,16);
} else {
if(hideMarker){
marker。 setVisible(false);
} else {
marker.setVisible(true);
}
}
}
});
}
使用以下代码也可以移动地图。
//在Google Map中显示当前位置
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
//放大Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
到目前为止,我所做的并不足以将标记和地图放在一起。它看起来并不完美。我必须将地图和标记一起移动。
谢谢。
为了达到这个目的,你必须稍微修改你的代码。
添加以下代码:
private static final int ANIMATE_SPEEED_TURN = 1000;
private static final int BEARING_OFFSET = 20;
if(t <1){
mHandler.postDelayed(this,16);
} else {
//你的代码
if(hideMarker){
marker.setVisible(false);
} else {
marker.setVisible(true);
$ b $ //我添加的代码
LatLng begin = getBeginLatLng(); //当前点
LatLng end = getEndLatLng(); //下一点
float bearingL = bearingBetweenLatLngs(begin,end);
CameraPosition cameraPosition =
CameraPosition.Builder()
.target(end)
.bearing(bearingL + BEARING_OFFSET)
.tilt(tilt)
.zoom(googleMap.getCameraPosition()。zoom)
.build();
googleMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPosition),
ANIMATE_SPEEED_TURN,
null
);
mHandler.postDelayed(动画师,16);
$ / code>
让我知道如果出现任何问题!!!
有关详细步骤,请访问制作地图动画
I have to show real time/live user moving location in google map once user turn on the feature and up-to terminating it.
I have had used the method below to animate the marker.
private 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 = 1000;
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);
}
}
}
});
}
And using the following code am moving the map too.
// Showing the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
// Zoom in the Google Map
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
What I had done so far isn't good enough to move the marker and map together. it's not looking that perfect. I have to move the map along with marker together.
Thank you.
- What I done in my similar project is like: Assume I have a list of point where user navigated one by one so, I want to display that trip in map with animation. Instead of moving both marker and camera same time you can move marker between two points and then animate camera to that second point, now again move marker to next point and then when marker reach out next point animate your camera.
To get this working you have to modify your code little bit. Add this code:
private static final int ANIMATE_SPEEED_TURN = 1000;
private static final int BEARING_OFFSET = 20;
if (t < 1) {
mHandler.postDelayed(this, 16);
} else {
// your code
if (hideMarker) {
marker.setVisible(false);
} else {
marker.setVisible(true);
}
//my added code
LatLng begin = getBeginLatLng();// current point
LatLng end = getEndLatLng();// next point
float bearingL = bearingBetweenLatLngs(begin, end);
CameraPosition cameraPosition =
new CameraPosition.Builder()
.target(end)
.bearing(bearingL + BEARING_OFFSET)
.tilt(tilt)
.zoom(googleMap.getCameraPosition().zoom)
.build();
googleMap.animateCamera(
CameraUpdateFactory.newCameraPosition(cameraPosition),
ANIMATE_SPEEED_TURN,
null
);
mHandler.postDelayed(animator, 16);
}
Let me know If anything goes wrong!!! For detailed step visit Animating the map
这篇关于Google地图:移动标记和地图一起与用户一起顺利吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!