使用android上的航点绘制驾驶路线(谷歌地图,谷歌方向API,JSON解析,解码谷歌折线) [英] drawing driving routes using waypoints on android ( Google maps, Google direction api, json parsing, decode google polyline)

查看:115
本文介绍了使用android上的航点绘制驾驶路线(谷歌地图,谷歌方向API,JSON解析,解码谷歌折线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

drawPath()用于在地图上绘制多边形。

  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);
字符串encodedString = overviewPolylines.getString(points);

String statusString = jsonObject.getString(status);

Log.d(test:,encodedString);
列表< 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());
}
}

私人列表< LatLng> decodePoly(字符串编码){


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 + = 5;
} while(b> = 0x20); $(结果& 1)!= 0?〜(结果>> 1):(结果>> 1));
latitude + = destLat;

shift = 0;
结果= 0;
do {
b = encoded.charAt(index ++) - 63;
result | =(b& 0x1f)<<转移;
shift + = 5;
} while(b> 0x20); (结果& 1)!= 0?〜(结果>> 1):(结果>> 1));(b)
longitude + = destLong;

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





$ b现在我的问题是路由在地图上显示不正确,因为它有路标在链接中。我在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=通过:18.XXX,73.XXX

解决方案

我已经测试了您的decodePoly部分。在那里似乎没有问题。你可以使用步骤和腿来更精确。

drawPath() is used to draw polyloine on map.

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;
    }

Now my problem is route is not displayed correctly in the map as it has waypoints in the link. I am using it on android 4.2

My link is 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

解决方案

I have tested your decodePoly part. seems no problem there. you can use steps and legs to be more precise.

这篇关于使用android上的航点绘制驾驶路线(谷歌地图,谷歌方向API,JSON解析,解码谷歌折线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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