Je rencontre un problème lors du dessin d'un itinéraire lors d'un onLocationChanged().Mise à jour de MapView à partir de onLocationChanged()
Donc ce que j'essaie de faire est: J'ai une épingle (basée sur carOverlayItem) sur la carte et MyLocationOverlay montrant ma position actuelle. Je veux tracer une route entre ces deux points. Ainsi, chaque fois que l'utilisateur se déplace (nous recevons l'emplacement et que la méthode MyLocationOverlay.onLocationChanged() est déclenchée), j'obtiens des coordonnées de Google dans le fichier klm, l'analysant et remplissant un tableau avec des objets GeoPoint. Après que je suis en train de parcourir ce tableau GeoPoint et ajouter des superpositions avec méthode draw() Écrasement à 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);
}
}
Et voici ma classe 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);
}
}
J'ai lu Internet et est venu avec l'idée que j'ai besoin de remplir la variable routeNodes lorsque onLocationChanged(), puis appelez mapView.invalidate() pour dessiner une route dans la méthode onDraw() MapView, mais confronté à un problème que je ne sais pas comment transférer routeNodes variable et intentions n'est pas une option ici, si je comprends bien.
Aussi, peut-être, que MyLocationOverlay avec la méthode onLocationChanged() s'exécute dans thread pas UI et c'est pourquoi je ne peux pas dessiner sur la carte, mais dans ce cas, je pense, je devrais avoir une erreur, n'est pas jeté. Je suis confus et peux trouver n'importe quelle solution.
Toute aide serait appréciée.
Merci.
Comment avez-vous implémenté la classe GMapRouteHttpRequest? S'il vous plaît aider ... – scamexdotexe