【发布时间】:2012-08-07 12:17:57
【问题描述】:
我必须将 KML 文件绘制到 MapView 中。我看了 在互联网上,但我没有找到如何做到这一点的例子, 如果有人可以举例说明如何做到这一点,那就太好了!
【问题讨论】:
标签: android google-maps android-mapview kml android-maps
我必须将 KML 文件绘制到 MapView 中。我看了 在互联网上,但我没有找到如何做到这一点的例子, 如果有人可以举例说明如何做到这一点,那就太好了!
【问题讨论】:
标签: android google-maps android-mapview kml android-maps
现在不支持 KML。您可以在没有 KML 的情况下绘制这样的轨迹:
1) 向 Google 服务发出请求:
Request : http://maps.googleapis.com/maps/api/directions/output?parameters Info about : https://developers.google.com/maps/documentation/directions/
2) 发送请求
3) 像这样解析 JSON 响应:
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 有很多点可以通过这个解码:Map View draw directions using google Directions API - decoding polylines
5) 像这样绘制叠加层:
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;
}
}
祝你好运!
【讨论】:
Google 已停止处理 kml 文件,首选解析 xml 或 json。
【讨论】:
从 2012 年 7 月 27 日起,这种通过解析 KML 文件从 Google 中提取 Google 路线的方式不再可用(因为 Google 更改了检索 Google 路线的结构,现在您只能通过 JSON 或 XML 获取),它是时候使用 JSON 而不是 KML。
在我自己的问题here 中查看答案。
【讨论】: