我该如何利用Android中一个图形页面KML文件? [英] How do I draw a KML file on a MapView in Android?

查看:150
本文介绍了我该如何利用Android中一个图形页面KML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要画一个KML文件转换成一个图形页面。我看了 在互联网上,但我没有找到一个例子,如何做到这一点, 如果有人能举个例子怎么做,它西港岛线是伟大的!

解决方案

不支持KML现在。你可以画迹像W / O KML:

1)制作要求谷歌服务:

 请求:http://maps.googleapis.com/maps/api/directions/output?parameters
信息关于:https://developers.google.com/maps/documentation/directions/
 

2)发送查询请求

3)这样的解析JSON响应:

 的JSONObject的JSONObject;
  ...
JSONArray结果= jsonObject.optJSONArray(路);
JSONObject的路线= results.optJSONObject(0);
JSONArray腿= route.optJSONArray(腿);
的JSONObject腿= legs.optJSONObject(0);
JSONArray步骤= leg.optJSONArray(步);

的for(int i = 0; I< steps.length(); ++ I){
    的JSONObject步骤= steps.optJSONObject(ⅰ);
    JSONObject的startP = step.optJSONObject(START_LOCATION);
    的JSONObject ENDP = step.optJSONObject(END_LOCATION);
    JSONObject的折线= step.optJSONObject(折线);
    字符串连接codedPoints = polyline.optString(分);
    ...
 

4)连接codedPoints 有很多点,你可以去code。通过这样的:<一href="http://stackoverflow.com/questions/6708408/map-view-draw-directions-using-google-directions-api-decoding-polylines">Map使用谷歌路线API视图中绘制方向 - 解码折线

5)绘制覆盖是这样的:

 私有类路延伸覆盖{
        私人的ArrayList&LT;的GeoPoint&GT;清单;
        民营涂料粉刷;

        公共道路(ArrayList中&LT;的GeoPoint&GT;列表){
            this.list =新的ArrayList&LT;的GeoPoint&GT;();
            this.list.addAll(名单);
            油漆=新的油漆();
            paint.setColor(Color.MAGENTA);
            paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(4);
        }

        @覆盖
        公共无效画(油画画布,图形页面图形页面,布尔影子){
            drawPath(图形页面,帆布);
        }

        私人无效drawPath(MapView的MV,帆布油画){
            INT X1 = -1;
            INT Y1 = -1;
            INT×2 = -1;
            INT Y2 = -1;

            点对点=新的点();

            的for(int i = 0; I&LT;则为list.size();我++){
                mv.getProjection()toPixels(list.get(i)中,点)。
                X2 = point.x;
                Y2 = point.y;

                如果(ⅰ大于0){
                    canvas.drawLine(X1,Y1,X2,Y2,漆);
                }

                ,X1 = X2;
                Y1 = Y2;
            }
        }
 

祝你好运!

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!

解决方案

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!

这篇关于我该如何利用Android中一个图形页面KML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆