Android 在地图V2上设置汽车标记移动动画
我正在用实时位置更新为汽车在街道上行驶设置动画。但是,无论我朝哪个方向移动,汽车都朝向正北,方向角返回0.0。有没有办法让我的车头朝着我的方向移动。这是我的密码Android 在地图V2上设置汽车标记移动动画,android,google-maps,android-animation,android-location,Android,Google Maps,Android Animation,Android Location,我正在用实时位置更新为汽车在街道上行驶设置动画。但是,无论我朝哪个方向移动,汽车都朝向正北,方向角返回0.0。有没有办法让我的车头朝着我的方向移动。这是我的密码 private void draw_current_location(Location currentlocation) { LatLng current = new LatLng(currentlocation.getLatitude(), currentlocation.getLongitude());
private void draw_current_location(Location currentlocation) {
LatLng current = new LatLng(currentlocation.getLatitude(), currentlocation.getLongitude());
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(current, 15);
map.animateCamera(cameraUpdate);
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) {
// TODO: Consider calling
return;
}
Location calculatedLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(locationManager.getProvider(LocationManager.GPS_PROVIDER).supportsBearing()) {
float bearing = calculatedLoc.bearingTo(currentlocation);
centeredMap(current, bearing);
}
}
void centeredMap(final LatLng lalng, float bearng){
Location l = new Location("");
l.setLatitude(lalng.latitude);
l.setLongitude(lalng.longitude);
View marker = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
if (customMarker != null) customMarker.remove();
customMarker = map.addMarker(new MarkerOptions()
.position(lalng)
.title("Title")
.snippet("Description")
.anchor(0.5f, 0.5f)
.flat(true)
.rotation(bearng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.customIMAGE)));
Toast.makeText(getApplicationContext(), String.valueOf(l.getBearing()), Toast.LENGTH_SHORT).show();
customMarker.setPosition(lalng);
animation.animateMarker(l, customMarker);
}
更新:现在轴承正在返回值。在我编辑代码之后
if(locationManager.getProvider(LocationManager.GPS_PROVIDER).supportsBearing()) {
Location calculatedLoc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
float bearing = calculatedLoc.bearingTo(currentlocation);
Toast.makeText(getApplicationContext(), String.valueOf(bearing), Toast.LENGTH_SHORT).show();
centeredMap(currentlocation, bearing);
}
但由于锚(.5f,.5f),标记器沿中心垂直轴旋转,但它无法识别我的汽车标记器的前部,因此它无法沿街道设置动画。这辆车只是一个面向北方的静态图像,在我移动时被拖走。任何帮助都将不胜感激。我让它工作起来了。我本来打算在这件事上挑刺的。我甚至想画多辆不同旋转的汽车,并用开关盒或其他东西在它们之间设置动画。但我没有走那么远。我有一辆静止的汽车,面向北方,锚定在(.5,.5)处,开了一段距离,然后它开始旋转并跟随我的方向。 1) 只有当第一个位置和第二个位置之间相差几秒钟时,轴承才准确。(位置越近,更新的方向越不准确)为此,最好抓取距离越远的位置,并根据获得的方向设置方向动画;这不是实时动画(有一些延迟,但是看起来像真实的伟大动画)。
2) 您必须使用GPS提供商获取方位。我让它工作。我本来打算在这件事上挑刺的。我甚至想画多辆不同旋转的汽车,并用开关盒或其他东西在它们之间设置动画。但我没有走那么远。我有一辆静止的汽车,面向北方,锚定在(.5,.5)处,开了一段距离,然后它开始旋转并跟随我的方向。 1) 只有当第一个位置和第二个位置之间相差几秒钟时,轴承才准确。(位置越近,更新的方向越不准确)为此,最好抓取距离越远的位置,并根据获得的方向设置方向动画;这不是实时动画(有一些延迟,但是看起来像真实的伟大动画)。 2) 您必须使用GPS提供程序获取方位