3

onLocationChanged()のときにルートを描画する際に問題が発生します。

つまり、私がやろうとしているのは、地図上に(carOverlayItemに基づく)ピンがあり、MyLocationOverlayが現在の位置を示しているということです。この2点の間にルートを描きたいです。したがって、ユーザーが移動するたびに(場所を受け取り、MyLocationOverlay.onLocationChanged()メソッドがトリガーされる)、klmファイルでGoogleから座標を取得し、それを解析して、GeoPointオブジェクトで配列を埋めます。そのGeoPoint配列を反復処理して、Overwritten 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変数とインテントを転送する方法を知っていることはここではオプションではありません。

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

どんな助けでもいただければ幸いです。

ありがとう。

4

3 に答える 3

2

UIスレッドでネットワークデータを消費するときは注意してください。これはAndroid4.0以降では機能しません。

AsyncTaskを使用する必要があります

于 2012-11-25T19:35:52.083 に答える
1

実際、私のバージョンも機能しています。Googleが最初に経度を返し、次に緯度を返すことに気づきました。そのため、klmファイルの解析中にパラメーターが混乱したために問題が発生しました。だから私も+1=)

于 2011-09-14T07:06:45.337 に答える
0

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

@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()
   }
}
于 2011-09-13T10:35:53.487 に答える