2016-02-17 44 views
0

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

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),但它不承认我的车标志的前面,所以它不会沿街动画。汽车只是朝北的静态图像,在我移动时被拖动。任何帮助将非常感激。

回答

0

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