【问题标题】:Draw routes path based on my own markers in android Google maps v2在 android Google maps v2 中根据我自己的标记绘制路线路径
【发布时间】:2016-04-06 16:10:58
【问题描述】:

我在 Uni 校园里做关于公交车跟踪器的 android 项目。我需要画一条从一个公交车站到另一个公交车站的路线,然后再回到起点公交车站。(我的大学大约有 20 个公交车站,需要绘制路线让第一个用户知道公交车的路线)。

我设法按照本教程 here 绘制了一条路径

在另一个教程中,我发现了一个博客,该博客在 3 个位置的谷歌地图上教授绘制路径。示例代码是

private static final LatLng LOWER_MANHATTAN = new LatLng(40.722543,-73.998585);
private static final LatLng BROOKLYN_BRIDGE = new LatLng(40.7057, -73.9964);
private static final LatLng WALL_STREET = new LatLng(40.7064, -74.0094);

private String getMapsApiDirectionsUrl() {
    String waypoints = "waypoints=optimize:true|"
            + LOWER_MANHATTAN.latitude + "," + LOWER_MANHATTAN.longitude
            + "|" + "|" + BROOKLYN_BRIDGE.latitude + ","
            + BROOKLYN_BRIDGE.longitude + "|" + WALL_STREET.latitude + ","
            + WALL_STREET.longitude;

    String sensor = "sensor=false";
    String params = waypoints + "&" + sensor;
    String output = "json";
    String url = "https://maps.googleapis.com/maps/api/directions/"
            + output + "?" + params;
    return url;
}

但我得到一个错误是:

java.io.FileNotFoundException: https://maps.googleapis.com/maps/api/directions/json?waypoints=optimize:true|2.920114,101.775244||2.919726,101.771172|2.92234,101.769628&sensor=false
E/Background Task: java.lang.NullPointerException
E/ParserTask: errororg.json.JSONException: End of input at character 0 of

博客教程与链接相同。只是不同

String waypoints = "waypoints=optimize:true|"
            + LOWER_MANHATTAN.latitude + "," + LOWER_MANHATTAN.longitude
            + "|" + "|" + BROOKLYN_BRIDGE.latitude + ","
            + BROOKLYN_BRIDGE.longitude + "|" + WALL_STREET.latitude + ","
            + WALL_STREET.longitude;

和

/ Origin of route
String str_origin = "origin="+origin.latitude+","+origin.longitude;

// Destination of route
String str_dest = "destination="+dest.latitude+","+dest.longitude;

谁能给我一些关于多标记绘制路径的教程或指导?提前致谢!

【问题讨论】:

  • 可能是您的链接无效,上面写着Invalid request. Missing the 'origin' parameter. 检查是否使用origin 和destination 参数正确创建了网址。
  • 我认为您需要将起点定义为 START,将航点定义为巴士站,将目的地定义为 FINISH 路线。像这个例子:maps.googleapis.com/maps/api/directions/…
  • @ELITE 我已经尝试使用 origin 和 destination 参数创建好,它的工作!
  • @Gorio 我试试你的例子,谢谢!
  • @Gorio,我对 JSON 格式不是很熟悉,能否提供 .java 文件中的示例代码?提前致谢。

标签: android google-maps


【解决方案1】:

首先你需要修正你的方法

private static final LatLng LOWER_MANHATTAN = new LatLng(40.722543,-73.998585);
private static final LatLng BROOKLYN_BRIDGE = new LatLng(40.7057, -73.9964);
private static final LatLng WALL_STREET = new LatLng(40.7064, -74.0094);

private String getMapsApiDirectionsUrl() {
    String origin = "origin=" + LOWER_MANHATTAN.latitude + "," + LOWER_MANHATTAN.longitude;
    String waypoints = "waypoints=optimize:true|" + BROOKLYN_BRIDGE.latitude + "," + BROOKLYN_BRIDGE.longitude + "|";
    String destination = "destination=" + WALL_STREET.latitude + "," + WALL_STREET.longitude;

    String sensor = "sensor=false";
    String params = origin + "&" + waypoints + "&"  + destination + "&" + sensor;
    String output = "json";
    String url = "https://maps.googleapis.com/maps/api/directions/"
            + output + "?" + params;
    return url;
}

为了获得绘制路径,我遵循了这个答案https://stackoverflow.com/a/14702636/2697368

一旦你得到 JSON,就去做

public void drawPath(String result) {

    try {
        //Tranform the string into a json object
        final JSONObject json = new JSONObject(result);
        JSONArray routeArray = json.getJSONArray("routes");
        JSONObject routes = routeArray.getJSONObject(0);
        JSONObject overviewPolylines = routes.getJSONObject("overview_polyline");
        String encodedString = overviewPolylines.getString("points");
        List<LatLng> list = decodePoly(encodedString);

        Polyline line = mMap.addPolyline(new PolylineOptions()
                .addAll(list)
                .width(12)
                .color(Color.parseColor("#05b1fb"))//Google maps blue color
                .geodesic(true)
        );

    } catch (JSONException e) {

    }
}

private List<LatLng> decodePoly(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;
}

【讨论】:

  • 我设法创建了航路点,但是路径被跳过了一些标记它去了最短距离到达目的地。
  • 我不认为 Directions Api 跳过了您告诉他作为航点或起点或目的地的标记。我认为方向会根据您的点优化路线。如果您想要替代路线,请参阅有关如何获取路线的文档developers.google.com/maps/documentation/directions/…
  • 是的,谢谢你的解释。如果 Direction 是优化路线,它会变成与我的 Uni 校园巴士路线不同的路线,所以我必须想办法添加替代路线。
  • 我可以问你一些额外的事情吗?获得 JSON 后,我可以保存到 sqlite 吗?因为如果我的手机离线,我已经尝试过我的应用程序,我的应用程序将在尝试获取 JSON 后强制关闭。所以我想一旦我得到我保存到 sqlite 的数据,下次打开应用程序时就不需要互联网连接来绘制路径,只需从 sqlite 数据库中检索。
  • 在这里查看我对 sqlite 的回答stackoverflow.com/questions/36461113/…
【解决方案2】:

有时会在使用点生成错误的 url 时发生。 愿此功能有所帮助。 它对我有用,所以我分享了..

public static  String getMapsApiDirectionsUrl( LatLng start, LatLng End ) {
    String waypoints = "waypoints=optimize:true|"
            + start.latitude + "," + start.longitude
            + "|" + "|" +  End.latitude + ","
            + End.longitude;
    String OriDest = "origin="+start.latitude+","+start.longitude+"&destination="+End.latitude+","+End.longitude;

    String sensor = "sensor=false";
    String params = OriDest+"&%20"+waypoints + "&" + sensor;
    String output = "json";
    String url = "https://maps.googleapis.com/maps/api/directions/"
            + output + "?" + params;
    return url;
}

谢谢。

【讨论】:

    猜你喜欢
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-01-20
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多