Issue
I have to draw a KML file into a MapView. I looked in the internet but I didn't find an example how to do it, if someone can give an example how to do it it wil be great!
Solution
KML is not supported now. You can draw trace like that w/o KML :
1) Make request to Google service :
Request : http://maps.googleapis.com/maps/api/directions/output?parameters Info about : https://developers.google.com/maps/documentation/directions/
2) Send request
3) Parsing JSON response like this :
JSONObject jsonObject;
...
JSONArray results = jsonObject.optJSONArray("routes");
JSONObject route = results.optJSONObject(0);
JSONArray legs = route.optJSONArray("legs");
JSONObject leg = legs.optJSONObject(0);
JSONArray steps = leg.optJSONArray("steps");
for (int i=0; i < steps.length(); ++i) {
JSONObject step = steps.optJSONObject(i);
JSONObject startP = step.optJSONObject("start_location");
JSONObject endP = step.optJSONObject("end_location");
JSONObject polyline = step.optJSONObject("polyline");
String encodedPoints = polyline.optString("points");
...
4) encodedPoints has many points that you can decode by this : Map View draw directions using google Directions API - decoding polylines
5) Draw overlay like this :
private class Road extends Overlay {
private ArrayList<GeoPoint> list;
private Paint paint;
public Road(ArrayList<GeoPoint> list) {
this.list = new ArrayList<GeoPoint>();
this.list.addAll(list);
paint = new Paint();
paint.setColor(Color.MAGENTA);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(4);
}
@Override
public void draw(Canvas canvas, MapView mapView, boolean shadow) {
drawPath(mapView, canvas);
}
private void drawPath(MapView mv, Canvas canvas) {
int x1 = -1;
int y1 = -1;
int x2 = -1;
int y2 = -1;
Point point = new Point();
for (int i=0; i < list.size(); i++) {
mv.getProjection().toPixels(list.get(i), point);
x2 = point.x;
y2 = point.y;
if (i > 0) {
canvas.drawLine(x1, y1, x2, y2, paint);
}
x1 = x2;
y1 = y2;
}
}
Good luck!
Answered By - Ilya Demidov
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.