I enliven a car moving along the street with real-time location updates. But the car is facing true north, and the bearing returns 0.0 regardless of which direction Iโm moving. Is there any hack for the front of my car to move the direction I'm moving. Here is my code.
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); }
UPDATE: now the bearing returns values. After I edited my code as such.
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); }
But the marker rotates along the central vertical axis due to the anchor (.5f, .5f), but it DOES NOT RECOGNIZE my front car marker so that it does not animate along the street. A car is just a static image facing north, and it is pulled when I move. Any help would be greatly appreciated.
source share