2016-09-15 24 views
1

mapbox sdkを使用してマップにバスの位置を示すアンドロイドアプリを作成しています。 Uberアプリのように、位置に基づいてマーカーを回転したいと思います。 どうすればいいですか?MapBoxのマーカーの方向アンドロイド

コード:

IconFactory iconFactory = IconFactory.getInstance(navigationActivity.this); 
    Drawable iconDrawable = ContextCompat.getDrawable(navigationActivity.this, R.drawable.bus); 
    Icon icon = iconFactory.fromDrawable(iconDrawable); 
    map.clear(); 
    CameraPosition position = new CameraPosition.Builder() 
      .target(new LatLng(lat,lon)) // Sets the new camera position 
      .zoom(16) // Sets the zoom 
      .bearing(180) // Rotate the camera 
      .tilt(30) // Set the camera tilt 
      .build(); // Creates a CameraPosition from the builder 
    map.animateCamera(CameraUpdateFactory 
      .newCameraPosition(position), 7000); 
    final Marker marker = map.addMarker(new MarkerOptions() 
      .position(new LatLng(lat,lon)) 
      .title("You!") 
      .snippet("YOu are Currently here.")); 
    marker.setIcon(icon); 
+1

あなたが直面しているどのような問題は言及しなかった。

// Make sure you are using marker views so you can update the rotation. marker.setRotation((float) computeHeading(marker.getPosition(), position)); ... public static double computeHeading(LatLng from, LatLng to) { // Compute bearing/heading using Turf and return the value. return TurfMeasurement.bearing( Position.fromCoordinates(from.getLongitude(), from.getLatitude()), Position.fromCoordinates(to.getLongitude(), to.getLatitude()) ); } 

また、私が以前にターフ前に使用されるこのメソッドを使用することができます!すでにベアリングとチルト機能がコード内に実装されています – Stallion

+2

地図がロードされると、それはアニメートされ、回転します。別の地平線道路に別の場所が来ると、バスアイコンはその道路に移動しますが、マーカーの画像は..水平になるようにする必要があります@Stallion –

答えて

3

はここだけ、あなたの代わりにバスの、それはリアルタイムで国際宇宙ステーションを追跡以外を求めていないものをan exampleです。見出しの計算はTurfとMapbox Android Services SDKを使用して行われますが、その単一のメソッドだけが必要な場合は、ライブラリからメソッドをコピーすることができます。ここで私は、上記の例から、重要なコードです。

// Returns the heading from one LatLng to another LatLng. Headings are. Expressed in degrees 
// clockwise from North within the range [-180,180). The math for this method came from 
// http://williams.best.vwh.net/avform.htm#Crs I only converted it to Java. 
public static double computeHeading(LatLng from, LatLng to) { 
    double fromLat = Math.toRadians(from.getLatitude()); 
    double fromLng = Math.toRadians(from.getLongitude()); 
    double toLat = Math.toRadians(to.getLatitude()); 
    double toLng = Math.toRadians(to.getLongitude()); 
    double dLng = toLng - fromLng; 
    double heading = Math.atan2(Math.sin(dLng) * Math.cos(toLat), 
      Math.cos(fromLat) * Math.sin(toLat) - Math.sin(fromLat) * Math.cos(toLat) * Math.cos(dLng)); 
    return (Math.toDegrees(heading) >= -180 && Math.toDegrees(heading) < 180) ? 
      Math.toDegrees(heading) : ((((Math.toDegrees(heading) + 180) % 360) + 360) % 360 + -180); 
} 
+2

まだ方向を指していない2番目の機能を試しました。 –

+2

ちょっと私の間違い、私は同じ座標を与えられました。しかし、ここで私が直面しているもう一つの問題は、同じ方向であってもマーカが回転する毎にマーカーが変わるたびにです。 –

+0

毎回回転してはいけません。上のコードの唯一の問題点は、反時計回りに回転させてもマーカーが時計回りに常に回転することです。これを解決するには、どの方向を回転するかを調べるためのチェックが必要です。 – cammace

関連する問題