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スレッドで実行されていないため、マップ上に描画できない理由が考えられますが、この場合はエラーが発生するはずですスローされません。私は混乱しており、あらゆる解決策を見つけることができます。
ご協力いただければ幸いです。
ありがとうございました。
を使用する必要がありますか?助けてください... – scamexdotexe