Google 方向 API:获取当前步骤 JSON-对象到 Android 上的当前位置
Google Directions API: Get the current step JSON-Object to current position on Android
我正在使用 Google 中的路线 API。现在我想确定用户当前位置的当前步骤。
这是来自 google 网站的示例:
{
"status": "OK",
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJ7cv00DwsDogRAMDACa2m4K8",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ69Pk6jdlyIcRDqM1KDY3Fpg",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJgdL4flSKrYcRnTpP0XQSojM",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
"types" : [ "locality", "political" ]
}
],
"routes": [ {
"summary": "I-40 W",
"legs": [ {
"steps": [ {
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8507300,
"lng": -87.6512600
},
"end_location": {
"lat": 41.8525800,
"lng": -87.6514100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy@P"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS
Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
},
...
... additional steps of this leg
...
... additional legs of this route
"duration": {
"value": 74384,
"text": "20 hours 40 mins"
},
"distance": {
"value": 2137146,
"text": "1,328 mi"
},
"start_location": {
"lat": 35.4675602,
"lng": -97.5164276
},
"end_location": {
"lat": 34.0522342,
"lng": -118.2436849
},
"start_address": "Oklahoma City, OK, USA",
"end_address": "Los Angeles, CA, USA"
} ],
"copyrights": "Map data ©2010 Google, Sanborn",
"overview_polyline": {
"points": "a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\|tNtqf@l{Yd_Fblh@rxo@b}@xxSfytA
blk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`
cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@loPpxq@bw_@v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb
~uF{cNd}xBjp]fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvHx`k@dse
@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_BflmA~yQftNboWzoAlzp@mz`@|}_
@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC"
},
"warnings": [ ],
"waypoint_order": [ 0, 1 ],
"bounds": {
"southwest": {
"lat": 34.0523600,
"lng": -118.2435600
},
"northeast": {
"lat": 41.8781100,
"lng": -87.6297900
}
}
} ]
}
我以为我可以使用 start_location 字段并确定当前位置与 start_location 字段之间的距离,但我想这是错误的。
有人对此有可行的解决方案吗?
任何帮助将不胜感激。
您需要从 "polyline"
部分解码 "points"
标签(来自文档:
polyline contains a single points object that holds an encoded
polyline representation of the step. This polyline is an approximate
(smoothed) path of the step.
在 Google 地图中可以找到更多关于折线编码的信息,例如here) of each step 和来自 "legs"
数组的腿:
"legs": [ {
"steps": [ {
...
"polyline": {
"points": "a~l~Fjk~uOwHJy@P" <--- need to decode this string
},
...
} ]
}]
例如通过 PolyUtil.decode()
of Maps SDK for Android Utility Library,您得到了一系列用于步骤的 LatLng。
比您需要通过所有步骤创建循环并通过 PolyUtil.isLocationOnPath()
调用找到包含具有当前位置坐标的点的步骤。类似的东西:
JSONObject jsonObject = new JSONObject(jsonStringBuilder.toString());
JSONArray legsArr = jsonObject.getJSONArray("legs");
// for each leg
for (int i = 0; i < legsArr.length(); i++) {
// get array of steps
JSONArray stepsArr = legsArr.getJSONObject(i).getJSONArray("steps");
// for each step
for (int j = 0; j < stepsArr.length(); j++) {
// get step
JSONObject step = stepsArr.getJSONObject(j);
JSONObject polyline = step.getJSONObject("polyline");
String encodedPoints = polyline.getString("points");
// decode encoded path to list of points LatLng
List<LatLng> decodedPoints = PolyUtil.decode(encodedPoints);
if (PolyUtil.isLocationOnPath(currentLocation, decodedPoints, true, 100)) {
// your currentLocation is on the step of route
// constant 100 - is tolerance in meters
}
}
}
如果您需要确定 currentLocation
的确切段,而不是整个 step
,您需要在步骤中通过所有段添加循环:
...
// decode encoded path to list of points LatLng
List<LatLng> decodedPoints = PolyUtil.decode(encodedPoints);
List<LatLng> segment = new ArrayList(2);
for (int k = 0; k < decodedPoints.size() - 1; k++) {
segment.clear();
segment.add(decodedPoints.get(k));
segment.add(decodedPoints.get(k+1));
if (PolyUtil.isLocationOnPath(currentLocation, segment, true, 100)) {
// your currentLocation is on the segment of step
}
}
注意!这只是方法描述,不是完整的功能代码。
我正在使用 Google 中的路线 API。现在我想确定用户当前位置的当前步骤。
这是来自 google 网站的示例:
{
"status": "OK",
"geocoded_waypoints" : [
{
"geocoder_status" : "OK",
"place_id" : "ChIJ7cv00DwsDogRAMDACa2m4K8",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJ69Pk6jdlyIcRDqM1KDY3Fpg",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJgdL4flSKrYcRnTpP0XQSojM",
"types" : [ "locality", "political" ]
},
{
"geocoder_status" : "OK",
"place_id" : "ChIJE9on3F3HwoAR9AhGJW_fL-I",
"types" : [ "locality", "political" ]
}
],
"routes": [ {
"summary": "I-40 W",
"legs": [ {
"steps": [ {
"travel_mode": "DRIVING",
"start_location": {
"lat": 41.8507300,
"lng": -87.6512600
},
"end_location": {
"lat": 41.8525800,
"lng": -87.6514100
},
"polyline": {
"points": "a~l~Fjk~uOwHJy@P"
},
"duration": {
"value": 19,
"text": "1 min"
},
"html_instructions": "Head \u003cb\u003enorth\u003c/b\u003e on \u003cb\u003eS
Morgan St\u003c/b\u003e toward \u003cb\u003eW Cermak Rd\u003c/b\u003e",
"distance": {
"value": 207,
"text": "0.1 mi"
}
},
...
... additional steps of this leg
...
... additional legs of this route
"duration": {
"value": 74384,
"text": "20 hours 40 mins"
},
"distance": {
"value": 2137146,
"text": "1,328 mi"
},
"start_location": {
"lat": 35.4675602,
"lng": -97.5164276
},
"end_location": {
"lat": 34.0522342,
"lng": -118.2436849
},
"start_address": "Oklahoma City, OK, USA",
"end_address": "Los Angeles, CA, USA"
} ],
"copyrights": "Map data ©2010 Google, Sanborn",
"overview_polyline": {
"points": "a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\|tNtqf@l{Yd_Fblh@rxo@b}@xxSfytA
blk@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@xfFbw~@dz{A~d{A|zOxbrBbdUvpo@`
cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@loPpxq@bw_@v|{CbtY~jGqeMb{iF|n\~mbDzeVh_Wr|Efc\x`Ij{kE}mAb
~uF{cNd}xBjp]fulBiwJpgg@|kHntyArpb@bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvHx`k@dse
@j|p@zpiAp|gEicy@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_BflmA~yQftNboWzoAlzp@mz`@|}_
@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC"
},
"warnings": [ ],
"waypoint_order": [ 0, 1 ],
"bounds": {
"southwest": {
"lat": 34.0523600,
"lng": -118.2435600
},
"northeast": {
"lat": 41.8781100,
"lng": -87.6297900
}
}
} ]
}
我以为我可以使用 start_location 字段并确定当前位置与 start_location 字段之间的距离,但我想这是错误的。 有人对此有可行的解决方案吗? 任何帮助将不胜感激。
您需要从 "polyline"
部分解码 "points"
标签(来自文档:
polyline contains a single points object that holds an encoded polyline representation of the step. This polyline is an approximate (smoothed) path of the step.
在 Google 地图中可以找到更多关于折线编码的信息,例如here) of each step 和来自 "legs"
数组的腿:
"legs": [ {
"steps": [ {
...
"polyline": {
"points": "a~l~Fjk~uOwHJy@P" <--- need to decode this string
},
...
} ]
}]
例如通过 PolyUtil.decode()
of Maps SDK for Android Utility Library,您得到了一系列用于步骤的 LatLng。
比您需要通过所有步骤创建循环并通过 PolyUtil.isLocationOnPath()
调用找到包含具有当前位置坐标的点的步骤。类似的东西:
JSONObject jsonObject = new JSONObject(jsonStringBuilder.toString());
JSONArray legsArr = jsonObject.getJSONArray("legs");
// for each leg
for (int i = 0; i < legsArr.length(); i++) {
// get array of steps
JSONArray stepsArr = legsArr.getJSONObject(i).getJSONArray("steps");
// for each step
for (int j = 0; j < stepsArr.length(); j++) {
// get step
JSONObject step = stepsArr.getJSONObject(j);
JSONObject polyline = step.getJSONObject("polyline");
String encodedPoints = polyline.getString("points");
// decode encoded path to list of points LatLng
List<LatLng> decodedPoints = PolyUtil.decode(encodedPoints);
if (PolyUtil.isLocationOnPath(currentLocation, decodedPoints, true, 100)) {
// your currentLocation is on the step of route
// constant 100 - is tolerance in meters
}
}
}
如果您需要确定 currentLocation
的确切段,而不是整个 step
,您需要在步骤中通过所有段添加循环:
...
// decode encoded path to list of points LatLng
List<LatLng> decodedPoints = PolyUtil.decode(encodedPoints);
List<LatLng> segment = new ArrayList(2);
for (int k = 0; k < decodedPoints.size() - 1; k++) {
segment.clear();
segment.add(decodedPoints.get(k));
segment.add(decodedPoints.get(k+1));
if (PolyUtil.isLocationOnPath(currentLocation, segment, true, 100)) {
// your currentLocation is on the segment of step
}
}
注意!这只是方法描述,不是完整的功能代码。