您好我正在尝试在Google地图中将标记从一个位置更新到另一个位置。我想用动画更改标记的位置,就像uber&奥拉。示例代码如下。我没有收到任何错误。我只是没有看到动画。任何简单的动画都可以。
代码
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static int SPLASH_TIME_OUT = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in tilak nagar
LatLng tilkanagar = new LatLng(28.6353, 77.0971);
LatLng Firstmarker = new LatLng(28.6350, 77.0970);
LatLng FirstmarkerTo = new LatLng(28.6356, 77.0972);
LatLng Secondmarker = new LatLng(28.6345, 77.0969);
LatLng SecondmarkerTo = new LatLng(28.6347, 77.0967);
mMap.addMarker(new MarkerOptions().position(tilkanagar).title("Hello I am at Techvision"));
Marker marker = mMap.addMarker(new MarkerOptions().position(Firstmarker).title("1 Minute Away").icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));
Marker marker1 = mMap.addMarker(new MarkerOptions().position(Secondmarker).title("2 Minutes Away").icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(tilkanagar));
mMap.animateCamera(CameraUpdateFactory.zoomTo(18.0f));
animateMarker(marker, FirstmarkerTo, false);
animateMarker(marker1,SecondmarkerTo,false);
}
public void animateMarker(final Marker marker, final LatLng toPosition,
final boolean hideMarker) {
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 5 seconds
sleep(5*1000);
// After 5 seconds redirect to another intent
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);
}
}
}
});
//Remove activity
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
}
输出
答案 0 :(得分:0)
您可以尝试通过标记选项设置动画。您可以使用setAnimation()
或者你可以使用interpolator
,插值器定义动画的变化率。这允许加速,减速,重复等基本动画效果(alpha,缩放,平移,旋转)。
var marker;
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);
}
}
}
});
}
这是一张相关的SO票,您可以尝试社区提供的工作:How to animate marker in android map api V2?