首页 文章

在 Map V2上动画汽车标记运动

提问于
浏览
0

我正在通过实时位置更新在街道上移动汽车 . 但是汽车面向真正的北方,无论我向哪个方向移动,轴承都会返回0.0 . 是否有任何黑客可以使我的车前方移动我正在移动的方向 . 这是我的代码 .

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),标记沿着中心垂直轴旋转,但它不能识别我的汽车标记的前部,因此它不会沿着街道动画 . 汽车只是朝北的静态图像,当我移动时被拖动 . 任何帮助将非常感激 .

1 回答

  • 0

    我得到了它的工作 . 我会在这个上拉我的头发 . 我甚至会使用开关盒或其他东西在不同的旋转和动画之间绘制多辆汽车 . 但我没那么走 . 我有一辆静止的汽车朝北,锚点在(.5,.5)并开到一个距离实际上很重要然后它开始旋转并按照我的方向行驶 . 1)如果第一个位置和第二个位置之间的差异很小,则轴承只是准确的 . (位置越接近,轴承的精确度越低)为此,最好 grab 更远的位置,并根据轴承的方向设置动画方向;这不是实时动画(有一些延迟但很棒的动画看起来像真的) . 2)你必须使用GPS提供商来获取方位 .

相关问题