gpt4 book ai didi

android - 谷歌路线 API : Get the current step JSON-Object to current position on Android

转载 作者:行者123 更新时间:2023-11-29 18:25:45 26 4
gpt4 key购买 nike

我正在使用 Google 的 Directions API。现在我想确定用户当前位置的当前步骤。

这是来自谷歌网站的示例:

{
"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 map 中多段线编码的更多信息,您可以找到例如here ) 每个 step"legs" 数组中的腿:

"legs": [ {
"steps": [ {
...
"polyline": {
"points": "a~l~Fjk~uOwHJy@P" <--- need to decode this string
},
...
} ]
}]

例如通过PolyUtil.decode()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
}
}

注意!这只是方法描述,不是完整的功能代码。

关于android - 谷歌路线 API : Get the current step JSON-Object to current position on Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59117996/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com