Animating car Marker movement on map V2

房东的猫 提交于 2019-12-11 10:18:02

问题


I am animating car moving on a street with real time location updates. But the car is facing to true north and the bearing returns 0.0 no matter which direction I am moving. Is there any hack to make the front of my car moving the direction I am 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 is returning 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 is rotating along the center vertical axis because of the anchor(.5f, .5f) but it doesn't RECOGNIZE my car marker's front so it doesn't animate along the street. The car is simply static image facing north and being dragged when I move. Any help would be much appreciated.


回答1:


I got it to work. I was gonna pull my hairs on this one. I was even gonna draw multiple cars with different rotation and animate between all using a switch case or something. But I didn't go that far. I had a static car facing north with anchor at (.5,.5) and drove to a distance to actually matter then it starts to rotate and follow my direction. 1) The bearing is only accurate if there is few seconds difference between the first location and the second. (The closer the location updates the less accurate the bearing)To do that it is better to grab locations further apart and animate direction based on the bearing you get; which is not real time animation(with some latency but great animation that looks like real). 2)You have to use GPS provider to get bearing.



来源:https://stackoverflow.com/questions/35449269/animating-car-marker-movement-on-map-v2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!