如何将Google Maps JSON文件读入Android Studio(Java)
我们正在使用谷歌地图API为我们在Android Studio中制作的一个应用程序工作。但是我们在读取JSON文件时遇到了问题。我们如何访问发送的所有信息 JSON文件片段:如何将Google Maps JSON文件读入Android Studio(Java),java,android,json,Java,Android,Json,我们正在使用谷歌地图API为我们在Android Studio中制作的一个应用程序工作。但是我们在读取JSON文件时遇到了问题。我们如何访问发送的所有信息 JSON文件片段: "routes" : [ { "bounds" : { "northeast" : { "lat" : 56.30786089999999, "lng" : 9.167680600000001 }, "southwes
"routes" : [
{
"bounds" : {
"northeast" : {
"lat" : 56.30786089999999,
"lng" : 9.167680600000001
},
"southwest" : {
"lat" : 56.13854869999999,
"lng" : 8.967453599999999
}
},
"copyrights" : "Kortdata ©2016 Google",
"legs" : [
{
"distance" : {
"text" : "7,6 km",
"value" : 7592
},
更多关于JSON文件的信息:
我们需要将所有的distance.value相加来计算总距离。到目前为止,我们只知道如何检索第一个距离。请记住,并非所有名为“距离”的值都相关
目前,我们可以通过从a到B的行程来实现,但如果我们添加一个航路点C,我们只能得到从a到B的距离,而不能得到从B到C的距离
片段:
private void parseJSon(String data) throws JSONException {
if (data == null)
return;
List<Route> routes = new ArrayList<Route>();
JSONObject jsonData = new JSONObject(data);
JSONArray jsonRoutes = jsonData.getJSONArray("routes");
for (int i = 0; i < jsonRoutes.length(); i++) {
JSONObject jsonRoute = jsonRoutes.getJSONObject(i);
Route route = new Route();
JSONObject overview_polylineJson = jsonRoute.getJSONObject("overview_polyline");
JSONArray jsonLegs = jsonRoute.getJSONArray("legs");
JSONObject jsonLeg = jsonLegs.getJSONObject(0);
JSONObject jsonDistance = jsonLeg.getJSONObject("distance");
JSONObject jsonDuration = jsonLeg.getJSONObject("duration");
JSONObject jsonEndLocation = jsonLeg.getJSONObject("end_location");
JSONObject jsonStartLocation = jsonLeg.getJSONObject("start_location");
route.distance = new Distance(jsonDistance.getString("text"), jsonDistance.getInt("value"));
route.duration = new Duration(jsonDuration.getString("text"), jsonDuration.getInt("value"));
route.endAddress = jsonLeg.getString("end_address");
route.startAddress = jsonLeg.getString("start_address");
route.startLocation = new LatLng(jsonStartLocation.getDouble("lat"), jsonStartLocation.getDouble("lng"));
route.endLocation = new LatLng(jsonEndLocation.getDouble("lat"), jsonEndLocation.getDouble("lng"));
route.points = decodePolyLine(overview_polylineJson.getString("points"));
routes.add(route);
}
listener.onDirectionFinderSuccess(routes);
}
private void parseJSon(字符串数据)抛出JSONException{
如果(数据==null)
返回;
列表路由=新建ArrayList();
JSONObject jsonData=新的JSONObject(数据);
JSONArray jsonRoutes=jsonData.getJSONArray(“routes”);
对于(int i=0;i
您是否已进入调试器或添加了一些日志以了解应用程序正在执行的操作?在这段代码中,我没有看到任何距离相加的情况。你是对的,我们不知道需要相加什么。我想我们需要使用JSONObject。比如:JSONObject jsonDistance=jsonLeg.getJSONObject(“距离”);但是我们只得到第一个距离值。好的,听起来你知道你需要做什么-有什么问题吗?