0
我是动画汽车在街道上移动的实时位置更新。但是汽车正面向真北方,无论我正在朝哪个方向移动,轴承都会返回0.0。有没有什么破解让我的车前方向我移动的方向移动。这是我的代码。动画汽车在地图上的标记运动V2
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),但它不承认我的车标志的前面,所以它不会沿街动画。汽车只是朝北的静态图像,在我移动时被拖动。任何帮助将非常感激。