2015-07-21 90 views
0

我需要在導航器中使用osmdroid製作MapView。駕駛員通過從服務器取走的路線駕駛汽車。由於路線始終位於地圖上他的位置,因此應該爲他旋轉地圖。問題是我無法爲每個旋轉找到正確的角度。有時它起作用,有時不起作用。例如,如何定義旋轉將在1米內?也許,是osmdroid中的一些庫或類可以幫助我解決這些問題?在osmdroid中通過路徑旋轉MapView

回答

0

我找到了解決方案。此方法計算出旋轉地圖的正確度數。我把我的路線切成直線。當前位置靠近下一行時,我稱這種方法爲

public double rotateToNextCheckPoint() { 
    int lineId = OfferPreference.getInstance().getCurrentLineId(); 
    if (rotates != null && lineId >= 0 && road.mRouteHigh.size() > 0) { 
     if (lineId < (rotates.size() - 1)) { 
      GeoPoint nextPoint = rotates.get(lineId).getLast(); 
      GeoPoint currPoint = rotates.get(lineId).getFirst(); 

      if (nextPoint == null || currPoint == null) { 
       return 0; 
      } 

      double lat1 = Math.toRadians(currPoint.getLatitude()); 
      double lon1 = Math.toRadians(currPoint.getLongitude()); 
      double lat2 = Math.toRadians(nextPoint.getLatitude()); 
      double lon2 = Math.toRadians(nextPoint.getLongitude()); 

      double cos1 = Math.cos(lat1); 
      double cos2 = Math.cos(lat2); 
      double sin1 = Math.sin(lat1); 
      double sin2 = Math.sin(lat2); 

      double delta = lon2 - lon1; 
      double deltaCos = Math.cos(delta); 
      double deltaSin = Math.sin(delta); 

      double x = (cos1 * sin2) - (sin1 * cos2 * deltaCos); 
      double y = deltaSin * cos2; 
      double z = Math.toDegrees(Math.atan((-y/x))); 
      if (x < 0) { 
       z += 180; 
      } 
      double z2 = (z + 180) % 360 - 180; 
      z2 = -Math.toRadians(z2); 

      double angleRad = z2 - (Math.PI * 2 * Math.floor(z2/(2 * Math.PI))); 
      double angle = Math.toDegrees(angleRad); 
      rotationGestureOverlay.onRotate(-(float) angle, false); 
      return angle; 
     } 
    } 
    return 0; 
} 
0

我認爲你要做的最好的是根據設備指南針和/或GPS標題定位地圖。 Osmdroid沒有內置任何東西。

當你從服務器獲得路線時,你能計算出你正在使用哪條腿嗎?如果是這樣,你應該能夠弄清楚什麼時候旋轉地圖。