【发布时间】:2020-03-09 22:18:03
【问题描述】:
如何解析 Google Directions API 的 API 响应以绘制从 A 点到 B 点的路线?我从我的网络中间件获得我的 API 响应。我需要为此创建一个 POJO 吗? RoboPOJOgenerator 是用于从 json 制作 POJO 的好工具吗?这个 json 是我的 okhttp 记录器的响应。
{
"data":[
{
"bounds":{
"northeast":{
"lat":14.5845178,
"lng":121.0601253
},
"southwest":{
"lat":14.5722047,
"lng":121.0535383
}
},
"copyrights":"Map data \u00a92019",
"legs":[
{
"distance":{
"text":"2.0 km",
"value":2013
},
"duration":{
"text":"11 mins",
"value":638
},
"end_address":"J Vargas Bus Stop, Epifanio de los Santos, Ortigas Center, Mandaluyong, Metro Manila, Philippines",
"end_location":{
"lat":14.5845178,
"lng":121.0569895
},
"start_address":"14/F Robinsons Cybergate Tower 1 Pioneer St. Metro Manila, Philippines",
"start_location":{
"lat":14.5722047,
"lng":121.0540196
},
"steps":[
{
"distance":{
"text":"70 m",
"value":70
},
"duration":{
"text":"1 min",
"value":13
},
"end_location":{
"lat":14.5725109,
"lng":121.0545851
},
"html_instructions":"Head <b>northeast</b> on <b>Pioneer St</b> toward <b>Reliance St</b><div style=\"font-size:0.9em\">Pass by HMR Trading Haus (on the left)</div>",
"polyline":{
"points":"gc}wAsjzaV]q@Ug@IW"
},
"start_location":{
"lat":14.5722047,
"lng":121.0540196
},
"travel_mode":"DRIVING"
},
{
"distance":{
"text":"0.2 km",
"value":210
},
"duration":{
"text":"1 min",
"value":42
},
"end_location":{
"lat":14.5741085,
"lng":121.0535383
},
"html_instructions":"Turn <b>left</b> onto <b>Reliance St</b>",
"maneuver":"turn-left",
"polyline":{
"points":"ee}wAenzaVmBhAmAp@cCtA"
},
"start_location":{
"lat":14.5725109,
"lng":121.0545851
},
"travel_mode":"DRIVING"
},
{
"distance":{
"text":"0.6 km",
"value":573
},
"duration":{
"text":"3 mins",
"value":159
},
"end_location":{
"lat":14.5780025,
"lng":121.0569962
},
"html_instructions":"Turn <b>right</b> onto <b>Sheridan</b>",
"maneuver":"turn-right",
"polyline":{
"points":"eo}wAsgzaVU_@Yg@GKKKECeCwB_ByAu@s@YWuAcAqCmB[UWQ}@o@o@g@"
},
"start_location":{
"lat":14.5741085,
"lng":121.0535383
},
"travel_mode":"DRIVING"
},
{
"distance":{
"text":"0.8 km",
"value":829
},
"duration":{
"text":"6 mins",
"value":336
},
"end_location":{
"lat":14.5843678,
"lng":121.0599721
},
"html_instructions":"Continue onto <b>San Miguel Ave</b>",
"polyline":{
"points":"og~wAg}zaVOi@OMYWKGq@e@KIm@a@[SYSCAy@k@}@m@WSe@_@WOeAw@]Uw@m@}BgBKGQKMCMCM@S?_@BQ@G?y@BkAFw@?S?YBq@@S@}AB"
},
"start_location":{
"lat":14.5780025,
"lng":121.0569962
},
"travel_mode":"DRIVING"
},
{
"distance":{
"text":"0.3 km",
"value":331
},
"duration":{
"text":"1 min",
"value":88
},
"end_location":{
"lat":14.5845178,
"lng":121.0569895
},
"html_instructions":"Turn <b>left</b> onto <b>Do\u00f1a Julia Vargas Ave</b><div style=\"font-size:0.9em\">Destination will be on the right</div>",
"maneuver":"turn-left",
"polyline":{
"points":"io_xAyo{aVS@?PAd@?b@?\\AxEA`A?RA\\AlB?Z"
},
"start_location":{
"lat":14.5843678,
"lng":121.0599721
},
"travel_mode":"DRIVING"
}
],
"traffic_speed_entry":[
],
"via_waypoint":[
]
}
],
"overview_polyline":{
"points":"gc}wAsjzaVs@yAIWmBhAqEfCo@gASWkC{BuCmCoB{AcGeEo@g@Oi@i@e@wByAsBuAsCqByGcF]S[GsAFaABkAFw@?m@BeABqBDAv@C|ICzD"
},
"summary":"Sheridan and San Miguel Ave",
"warnings":[
],
"waypoint_order":[
]
}
],
"status":"SUCCESS"
}
这是我来自中间件的回应,我不能使用下面的代码
private String getRequestUrl(LatLng origin, LatLng dest) {
String str_org = "origin=" + origin.latitude + "," + origin.longitude;
String str_dest = "destination=" + dest.latitude + "," + dest.longitude;
String sensor = "sensor=false";
String mode = "mode=driving";
String avoid = "avoid=tolls|highways";
String param = str_org + "&" + str_dest + "&" + sensor + "&" + mode + "&" + avoid;
String output = "json";
String url = "https://maps.googleapis.com/maps/api/directions/" + output + "?" + param + "&key=" + getString(R.string.API_KEY);
return url;
}
private String requestDirection(String reqUrl) throws IOException {
String responseString = "";
InputStream inputStream = null;
HttpURLConnection httpURLConnection = null;
try {
URL url = new URL(reqUrl);
httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.connect();
//Get the response result
inputStream = httpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line);
}
responseString = stringBuffer.toString();
bufferedReader.close();
inputStreamReader.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
inputStream.close();
}
httpURLConnection.disconnect();
}
return responseString;
}
因为我没有在我的移动应用程序中访问 Directions API,所以我通过我的网络中间件访问它。我真的需要帮助我已经被这个问题困扰了 3 天了。抱歉问了太多问题。
【问题讨论】:
标签: java android json google-maps google-directions-api