Si vous avez juste besoin du point connecté à une ligne, vous n'avez pas besoin du kml complet. Une manière beaucoup plus rapide de le faire est d'utiliser simplement le JSON retour de l'API Google Maps avec sortie = dragdir
private String getUrl(String start, String end) {
//If params GeoPoint convert to lat,long string here
StringBuffer urlString = new StringBuffer();
urlString.append("http://maps.google.com/maps?f=d&hl=en");
urlString.append("&saddr=");// from
urlString.append(start);
urlString.append("&daddr=");// to
urlString.append(end);
urlString.append("&ie=UTF8&0&om=0&output=dragdir"); //DRAGDIR RETURNS JSON
Log.i("URLString", urlString.toString());
return urlString.toString();
}
Ce urlString peut être utilisé pour obtenir un fichier JSON, que vous pouvez facilement extraire les informations en utilisant la scission de chaîne()
private String getConnection(String url) {
URL inUrl = new URL(url);
URLConnection yc = inUrl.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
String inputLine;
String encoded = "";
while ((inputLine = in.readLine()) != null)
encoded = encoded.concat(inputLine);
in.close();
String polyline = encoded.split("points:")[1].split(",")[0];
polyline = polyline.replace("\"", "");
polyline = polyline.replace("\\\\", "\\");
return polyline;
}
la chaîne retournée est une polyligne qui peut être décodé dans une liste de géopoints en utilisant la méthode ci-dessous.
private static ArrayList<GeoPoint> decodePolyline(String encoded) {
ArrayList<GeoPoint> geopoints = new ArrayList<GeoPoint>();
int index = 0, len = encoded.length();
int lat = 0, lng = 0;
while (index < len) {
int b, shift = 0, result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = encoded.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
GeoPoint p = new GeoPoint((int) (((double) lat/1E5) * 1E6), (int) (((double) lng/1E5) * 1E6));
geopoints.add(p);
}
return geopoints;
}
La dernière étape consiste à attirer ces points à un Mapview, pour que nous avons besoin d'un élément de recouvrement qui gère un ArrayList de géopoints.
public class PathOverlay extends Overlay {
private ArrayList<GeoPoint> pointList;
public PathOverlay(ArrayList<GeoPoint> pointList) {
this.pointList = pointList;
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
Point current = new Point();
Path path = new Path();
Projection projection = mapView.getProjection();
Iterator<GeoPoint> iterator = pointList.iterator();
if (iterator.hasNext()) {
projection.toPixels(iterator.next(), current);
path.moveTo((float) current.x, (float) current.y);
} else return;
while(iterator.hasNext()) {
projection.toPixels(iterator.next(), current);
path.lineTo((float) current.x, (float) current.y);
}
Paint pathPaint = new Paint();
pathPaint.setAntiAlias(true);
pathPaint.setStrokeWidth(3.0f);
pathPaint.setColor(Color.GREEN);
pathPaint.setStyle(Style.STROKE);
canvas.drawPath(path, pathPaint);
}
}
Si vous n'êtes pas sûr de quelques-unes des étapes intermédiaires, telles que la façon d'obtenir la superposition sur le MapView ou comment mettre en place me laisser une classe de routeur sais et je peux vous envoyer le code complet.
Andrea, cette URL ne fournit pas de code d'exemple - semble l'enregistrement de domaine a expiré. Est-ce disponible ailleurs? –