动画在地图V2上的汽车标记移动 [英] Animating car Marker movement on map V2

查看:182
本文介绍了动画在地图V2上的汽车标记移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在动画在实时位置更新的街道上移动车辆。但是汽车正面向真北方,无论我正在朝哪个方向移动,轴承都会返回0.0。有没有什么破解让我的车前方向我移动的方向移动。这是我的代码。

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);
        }

但由于锚点(。),标记沿中心垂直轴旋转。 5f,.5f),但它不会识别我的汽车标记的前部,因此它不会沿着街道生动。汽车只是朝北的静态图像,在我移动时被拖动。任何帮助,将不胜感激。

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.

推荐答案

我得到它的工作。我要在这一个上拉我的头发。我甚至会用不同的旋转方式绘制多辆车,并在所有车辆之间使用开关盒或其他东西来制作动画。但我没有走得太远。我有一辆面向北方的静态汽车,在(.5,.5)处有一个锚点,然后驶向一段距离,然后开始转动并按照我的方向行驶。
1)如果第一个位置和第二个位置之间的秒数不同,那么方位是唯一准确的。 (位置越近,轴承的准确度越低)为此,最好抓住距离更远的位置,并根据所获得的方位为方向制作动画;这不是实时动画(有一些延迟,但看起来像是真实的动画)。
2)您必须使用GPS提供商以获取方位。

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.

这篇关于动画在地图V2上的汽车标记移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆