2011-09-13 71 views
3

當onLocationChanged()時,我有一個繪製路線的問題。從onLocationChanged更新MapView()

所以我想要做的是: 我有一個pin(基於carOverlayItem)在地圖上和MyLocationOverlay顯示我的當前位置。我想在這兩點之間畫出一條路線。 因此,每當用戶移動時(我們接收到位置並觸發MyLocationOverlay.onLocationChanged()方法),我都會從klm文件中獲取Google的座標,解析它並使用GeoPoint對象填充數組。之後,我試圖通過GeoPoint對象數組進行迭代,並添加疊加與被覆蓋的draw()方法來MapView類

public class GMapMyLocationOverlay extends MyLocationOverlay { 

    private MapView mapView; 
    private CarOverlayItem carOverlayItem = null; 
    private GeoPoint routeNodes[]; 

    public GMapMyLocationOverlay(Context context, MapView mapView, CarOverlayItem carOverlayItem) { 
     super(context, mapView); 
     this.mapView = mapView; 
     this.carOverlayItem = carOverlayItem; 
    } 

    @Override 
    public void onLocationChanged(Location location) { 
     // redraw route to the car point 
     if (!carOverlayItem.isEmpty()) { 
      GeoPoint fromLocation = new GeoPoint((int)(location.getLatitude() * 1e6), (int)(location.getLongitude() * 1e6)); 

      GMapRouteHttpRequest pointsRequest = new GMapRouteHttpRequest(fromLocation, carOverlayItem.getOverlayItem().getPoint()); 
      routeNodes = pointsRequest.getRoutePoints(); 

      // if the point is not set to be on the road, google can return empty points array 
      // in this case we will be drawing straight line between car position and current 
      // user's position on map 
      if (routeNodes != null && routeNodes.length > 0) { 
       for (int i = 1; i < routeNodes.length; i ++) { 
        mapView.getOverlays().add(new GMapRouteOverlay(routeNodes[i-1], routeNodes[i])); 
       } 
      } 
     } 
     super.onLocationChanged(location); 
    } 
} 

這裏是我的GMapRouteOverlay類

public class GMapRouteOverlay extends Overlay { 

    private GeoPoint fromPoint; 
    private GeoPoint toPoint; 

    public GMapRouteOverlay(GeoPoint fromPoint, GeoPoint toPoint) { 
     this.fromPoint = fromPoint; 
     this.toPoint = toPoint; 
    } 

    @Override 
    public void draw(Canvas canvas, MapView mapView, boolean shadow) { 
     Projection projection = mapView.getProjection(); 

     Paint paint = new Paint(); 
     paint.setColor(Color.RED); 
     paint.setStrokeWidth(5); 
     paint.setAntiAlias(true); 

     Point from = new Point(); 
     projection.toPixels(fromPoint, from); 

     Point to = new Point(); 
     projection.toPixels(toPoint, to); 

     Path path = new Path(); 
     path.moveTo(from.x, from.y); 
     path.lineTo(to.x, to.y); 
     canvas.drawPath(path, paint); 
     super.draw(canvas, mapView, shadow); 
    } 
} 

我讀過一些互聯網來到我想在onLocationChanged()時需要填充routeNodes變量,然後調用mapView.invalidate()在onDraw()MapView方法中繪製路線,但遇到了一個問題,我不知道如何傳遞routeNodes變量和intent根據我的理解,這裏不是一種選擇。

此外,可能是,MyLocationOverlay與onLocationChanged()方法在UI線程中運行,這就是爲什麼我不能在地圖上繪製,但在這種情況下,我認爲,我應該得到一個錯誤,這不被拋出。我很困惑,可以找到任何解決方案。

任何幫助,將不勝感激。

謝謝。

+0

您是如何實現GMapRouteHttpRequest類的?請幫助... – scamexdotexe

回答

1

其實,我的版本是工作壓力太大,我注意到,谷歌返回經度第一,然後才緯度,所以我與參數吻合,因爲混亂的問題,同時分析荷航文件。所以對我也+1 =)

+0

使用json格式獲取方向詳細信息而不是kml – Pratik

0

您可以通過調用類的靜態方法(此類由MapActivity擴展)來更新mapview,就像這樣。

@Override 
public void onLocationChanged(Location location) { 
    super.onLocationChanged(location); 
    MyMapClass.updateLocation(location); 
} 

並且在你的班級這樣做。

class MyMapClass extends MapActivity{ 
    private static MapView mapview; 
    public void onCreate(){} //// your oncreate() method 

    public static void updateLocation(Location location){ 
     // here you can get the location value using this location object now pass this value to draw the location or your current location 
     // and then call mapview.invalidate() 
    } 
} 
+0

怪異的...爲什麼我沒有得到它自己=)謝謝。 – Dmitry

+0

我發現什麼是錯的,所以在從klm文件中解析數據時要小心。 (請參閱下面的答案) – Dmitry

2

在UI線程上使用網絡數據時要小心,這在android 4.0及更高版本中不起作用。

您需要使用AsyncTask

+1

歡迎來到SO!更多的解釋和例子會使這個答案更好,我認爲:-) – poplitea