在地图V2上动画汽车标记运动

时间:2016-02-17 06:18:42

标签: android google-maps android-animation android-location

我正在通过实时位置更新在街道上移动汽车。但是汽车面向真正的北方,无论我向哪个方向移动,轴承都会返回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 :(得分:0)

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

相关问题