java - 使用 Androids Google Maps API 查找路线

标签 java android google-maps google-maps-api-3 google-maps-api-2

我希望能够使用适用于 Android 的 Google Maps API 显示两个用户定义 地理点之间的路线。我还希望能够让用户选择要显示的路线类型,无论是步行、骑自行车、汽车等。此外,我希望能够计算使用这条路线所需的时间和距离。我试过在网上搜索并查看其他 stackoverflow 问题,但无济于事。我该怎么做?我怎样才能编码这个。

//----编辑----//

我还想获取交通信息,如繁忙路线、拥堵情况等

最佳答案

这里有一些代码可以帮助您。

String url= 
"http://maps.googleapis.com/maps/api/directions/json?origin=" 
+ origin.latitude + "," + origin.longitude +"&destination=" 
+ destination.latitude + "," + destination.longitude + "&sensor=false";

要使用 androidhttpclient 获取数据,请执行以下操作:

HttpResponse response;
HttpGet request;
AndroidHttpClient client = AndroidHttpClient.newInstance("somename");

request = new HttpGet(url);
response = client.execute(request);

InputStream source = response.getEntity().getContent();
String returnValue = buildStringIOutils(source);

return returnValue;

buildStringIOUtils 在哪里:

private String buildStringIOutils(InputStream is) {
    try {
        return IOUtils.toString(is, "UTF-8");
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

然后您可以使用类似这样的方法从 JSON 响应中提取实际折线:

JSONObject result = new JSONObject(returnValue);
JSONArray routes = result.getJSONArray("routes");

                    long distanceForSegment = routes.getJSONObject(0).getJSONArray("legs").getJSONObject(0).getJSONObject("distance").getInt("value");

                    JSONArray steps = routes.getJSONObject(0).getJSONArray("legs")
                            .getJSONObject(0).getJSONArray("steps");

                    List<LatLng> lines = new ArrayList<LatLng>();

                    for(int i=0; i < steps.length(); i++) {
                        String polyline = steps.getJSONObject(i).getJSONObject("polyline").getString("points");

                        for(LatLng p : decodePolyline(polyline)) {
                            lines.add(p);
                        }
                    }

方法 decodePolyline 是这样的:

    /** POLYLINE DECODER - http://jeffreysambells.com/2010/05/27/decoding-polylines-from-google-maps-direction-api-with-java **/
    private List<LatLng> decodePolyline(String encoded) {

        List<LatLng> poly = new ArrayList<LatLng>();

        int index = 0, len = encoded.length();
        int lat = 0, lng = 0;

        while (index < len) {
            int b, shift = 0, result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lat += dlat;

            shift = 0;
            result = 0;
            do {
                b = encoded.charAt(index++) - 63;
                result |= (b & 0x1f) << shift;
                shift += 5;
            } while (b >= 0x20);
            int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
            lng += dlng;

            LatLng p = new LatLng((double) lat / 1E5, (double) lng / 1E5);
            poly.add(p);
        }

        return poly;
    }

然后您可以使用以下方法将多段线添加到 map :

Polyline polylineToAdd = mMap.addPolyline(new PolylineOptions().addAll(lines).width(3).color(Color.RED));

要更改模式,请将其添加到 url(参见 https://developers.google.com/maps/documentation/directions/): &mode=您的模式

驾驶(默认)表示使用道路网络的标准驾驶方向。

walking 请求通过人行道和人行道(如有)的步行路线。

骑自行车请求通过自行车道和首选街道(如有)的骑车路线。

transit 请求通过公共(public)交通路线(如有)的指示。

编辑: 关于“我还想获取交通信息,例如繁忙路线、拥堵情况等”。我没有研究过这个,但我的代码应该能让你很好地开始。

编辑2: 在 google directions api 中找到这个: “对于行车路线:Maps for Business 客户可以根据当前交通状况指定 departure_time 以接收行程持续时间。departure_time 必须设置为当前时间的几分钟内。”

关于java - 使用 Androids Google Maps API 查找路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15990721/

相关文章:

java - Android - 谷歌地图中的多个标记

javascript - 如何使用页面滚动将谷歌地图背景滚动/向下滑动到 View 中

google-maps - 从公司名称获取完整地址——Google Maps API

java - 给定两个不同的 3D 点,查找一组特定的相邻点(不是全部)

java - 权限被拒绝导致 "Installation failed due to invalid APK file"

java - 如何在java中定义应用程序的快捷键

android - 在 EditText 中添加图标以用于消息传递目的,例如 whatsapp 或 msn

java - 为什么无法将 editView 内容传递给方法?

android - 创建 MultiUserChat 时出现 "$XMPPErrorException: XMPPError: forbidden - auth"错误

android - 将 parse.com 库导入 Android studio 0.8.2