2011-09-13 4 views
3

onLocationChanged()のルートを描画する際に問題が発生しました。onLocationChanged()からMapViewを更新してください。

私がしようとしていることは次のとおりです。 マップ上に(carOverlayItemに基づいて)ピンがあり、現在の位置を示すMyLocationOverlayがあります。私はその2つのポイントの間にルートを描きたい。 したがって、ユーザーが移動して(MyLocationOverlay.onLocationChanged()メソッドがトリガーされるたびに)、私はklmファイルでGoogleから座標を取得し、解析して配列にGeoPointオブジェクトを埋め込みます。私はそのGeoPoint配列を反復処理し、上書きドロー(とオーバーレイを追加しようとしていた後)メソッドの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変数を設定し、onDraw()MapViewメソッドでrouteを描画するためにmapView.invalidate()を呼び出す必要がありますが、routeNodesの変数とインテントをどのように転送するのかわからないという問題に直面しました私が理解するように、ここではオプションではありません。

また、onLocationChanged()メソッドを使用したMyLocationOverlayがUIスレッドで実行されていないため、マップ上に描画できない理由が考えられますが、この場合はエラーが発生するはずですスローされません。私は混乱しており、あらゆる解決策を見つけることができます。

ご協力いただければ幸いです。

ありがとうございました。

+0

を使用する必要がありますか?助けてください... – scamexdotexe

答えて

1

実は、私のバージョンがあまりにも働いている、私はGoogleが経度最初だけにして緯度を返すので、KLMファイルを解析しながら、私はので、パラメータを使用して混乱の問題を抱えていたことに気づきました。私のためにも+1 =)

+0

kmlの代わりに方向の詳細を取得するためにjson形式を使用 – Pratik

0

このように、クラスの静的メソッド(このクラスはMapActivityによって拡張されています)を呼び出すことによってマップビューを更新できます。

このようにしてください。

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スレッドでネットワークデータを消費するときは注意してください。アンドロイド4.0以上では動作しません。

あなたはどのようにクラスGMapRouteHttpRequestを実装しましたAsyncTask

+1

ようこそ!もう少し説明と例があればこれはさらに良い答えになるでしょう: – poplitea

関連する問題