首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用android上的路点绘制驾驶路线(谷歌地图,谷歌方向api,json解析,解码谷歌折线)

使用android上的路点绘制驾驶路线(谷歌地图,谷歌方向api,json解析,解码谷歌折线)
EN

Stack Overflow用户
提问于 2013-03-14 18:36:03
回答 2查看 6.2K关注 0票数 8

drawPath()用于在地图上绘制折线。

代码语言:javascript
运行
复制
public void drawPath(String result){
        try {
            final JSONObject jsonObject = new JSONObject(result);

            JSONArray routeArray = jsonObject.getJSONArray("routes");
            JSONObject routes = routeArray.getJSONObject(0);


            JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
            String encodedString = overviewPolylines.getString("points");

            String statusString = jsonObject.getString("status");

            Log.d("test: ", encodedString);
            List<LatLng> list = decodePoly(encodedString);

            LatLng last = null;
            for (int i = 0; i < list.size()-1; i++) {
                LatLng src = list.get(i);
                LatLng dest = list.get(i+1);
                last = dest;
                Log.d("Last latLng:", last.latitude + ", " + last.longitude );
                Polyline line = mMap.addPolyline(new PolylineOptions()
                .add(new LatLng(src.latitude, src.longitude), new LatLng(dest.latitude, dest.longitude))
                .width(4)
                .color(Color.GREEN));
            }

            Log.d("Last latLng:", last.latitude + ", " + last.longitude );
        }catch (JSONException e){
            e.printStackTrace();
        }
        catch(ArrayIndexOutOfBoundsException e) {
            System.err.println("Caught ArrayIndexOutOfBoundsException: "+ e.getMessage());
        }
    }

    private List<LatLng> decodePoly(String encoded){


        List<LatLng> poly = new ArrayList<LatLng>();
        int index = 0;
        int length = encoded.length();

        int latitude = 0;
        int longitude = 0;

        while(index < length){
            int b;
            int shift = 0;
            int result = 0;

            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);

            int destLat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            latitude += destLat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b > 0x20);

            int destLong = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            longitude += destLong;

            poly.add(new LatLng((latitude / 1E5),(longitude / 1E5) ));
        }
        return poly;
    }

现在我的问题是路线不能正确地显示在地图上,因为它在链接中有路点。我在android 4.2上使用它

我的链接是http://maps.googleapis.com/maps/api/directions/json?origin=18.XXX,73.XXXdestination=18.XXX,73.XXX&sensor=false&units=metric&mode=driving&waypoints=via:18.XXX,73.XXX

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-03-25 19:03:11

我已经测试了您的decodePoly部件。这似乎没什么问题。你可以使用脚步和腿来更精确。

票数 2
EN

Stack Overflow用户

发布于 2013-04-05 07:53:59

@Zeratul感谢你在overview_polyline.points上解码代码。这很好用。顺便说一句,在获得LatLng的列表之后,你不需要遍历列表。只需使用PolylineOptions的addAll方法即可。

代码语言:javascript
运行
复制
List<LatLng> list = decodePoly(encodedString);
PolylineOptions po = new PolylineOptions();
po.addAll(list);
po.width(2).color(Color.BLUE);
Polyline line = getMap().addPolyline(po);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15406847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档