【发布时间】: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