How do I draw a KML file on a MapView in Android?

你。 提交于 2019-12-20 03:02:47

问题


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!


回答1:


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!




回答2:


Google has stopped working with kml files, it is preferred to parse xml or json.




回答3:


This way of extracting the Google Directions from Google by parsing the KML file is no longer available since 27 July 2012 (because Google has changed the structure of retrieving Google Directions, now you can only get it by JSON or XML), it is time to use JSON instead of KML.

See the answer in my own question here.



来源:https://stackoverflow.com/questions/11845790/how-do-i-draw-a-kml-file-on-a-mapview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!