How do I extract the coordinates from the response content in navigation

You json is structured like so:

{bbox:[...],features:[{bbox:[...],geometry:{coordinates:[[...],[...],[...]], type:.....}

so you want to get to:

features(list) - geometry(json) - coordinates(list) - listitem 1

1 Like

Path:

features
1
geometry
coordinates
1

gives you the first pair.

Then select items 1 and 2 from the pair.

The given path produces an error : "Attempt to get item number 2 of a list of length 1:[''']".
I've tried another method by segmenting the strings. It worked but It did not seem optimal.

Please post the actual text json here so we can test block forms...

{"bbox":[101.454941,3.109381,101.464353,3.132095],"features":[{"bbox":[101.454941,3.109381,101.464353,3.132095],"geometry":{"coordinates":[[3.129745,101.464109],[3.130096,101.464144],[3.130617,101.464206],[3.130978,101.464248],[3.131133,101.464266],[3.131669,101.464327],[3.131928,101.464353],[3.132012,101.463335],[3.131999,101.463094],[3.131898,101.462446],[3.131885,101.462138],[3.131915,101.461706],],"type":"LineString"},"properties":{"extras":{"roadaccessrestrictions":{"summary":[{"amount":94.31,"distance":4047.5,"value":0},{"amount":5.69,"distance":244.3,"value":4}],"values":[[0,6,4],[6,81,0]]}},"fare":0,"segments":[{"distance":4291.8,"duration":389.7,"steps":[{"distance":244.3,"duration":58.6,"instruction":"Head north","name":"-","type":11,"way_points":[0,6]},{"distance":1001.1,"duration":72.1,"instruction":"Turn left onto Jalan Setia Permai U13/40","name":"Jalan Setia Permai U13/40","type":0,"way_points":[6,26]},{"distance":417.5,"duration":33.4,"instruction":"Turn left onto Persiaran Setia Permai","name":"Persiaran Setia Permai","type":0,"way_points":[26,30]},{"distance":1420.8,"duration":94.7,"instruction":"Turn right","name":"-","type":1,"way_points":[30,49]},{"distance":560.9,"duration":51.6,"instruction":"Turn left onto Persiaran Setia Wawasan","name":"Persiaran Setia Wawasan","type":0,"way_points":[49,57]},{"distance":502,"duration":44.4,"instruction":"Turn right onto Persiaran Setia Dagang","name":"Persiaran Setia Dagang","type":1,"way_points":[57,71]},{"distance":27.2,"duration":6.5,"instruction":"Turn left","name":"-","type":0,"way_points":[71,73]},{"distance":32.8,"duration":7.9,"instruction":"Turn left","name":"-","type":0,"way_points":[73,75]},{"distance":28,"duration":6.7,"instruction":"Turn right","name":"-","type":1,"way_points":[75,76]},{"distance":28.4,"duration":6.8,"instruction":"Turn right","name":"-","type":1,"way_points":[76,78]},{"distance":28.9,"duration":6.9,"instruction":"Keep right","name":"-","type":13,"way_points":[78,81]},{"distance":0,"duration":0,"instruction":"Arrive at your destination, straight ahead","name":"-","type":10,"way_points":[81,81]}]}],"summary":{"distance":4291.8,"duration":389.7},"transfers":0,"warnings":[{"code":1,"message":"There may be restrictions on some roads"}],"way_points":[0,81]},"type":"Feature"}],"metadata":{"attribution":"openrouteservice.org | OpenStreetMap contributors","engine":{"build_date":"2023-07-09T01:31:50Z","graph_date":"2023-11-10T16:57:08Z","version":"7.1.0"},"query":{"coordinates":[[101.46336772591981,3.1298175408991913],[101.4576429,3.1094739]],"format":"geojson","language":"en","profile":"driving-car"},"service":"routing","timestamp":1699888800555},"type":"FeatureCollection"}

Here you go and thank you for offering help.

Unable to decode it?

image

I copied it directly from the result of the Navigation.ResponseContent. Perhaps I copied wrong?

{"bbox":[101.454941,3.109381,101.464353,3.132095],"features":[{"bbox":[101.454941,3.109381,101.464353,3.132095],"geometry":{"coordinates":[[3.129745,101.464109],[3.130096,101.464144],[3.130617,101.464206],[3.130978,101.464248],[3.131133,101.464266],[3.131669,101.464327],[3.131928,101.464353],[3.132012,101.463335],[3.131999,101.463094],[3.131898,101.462446],[3.131885,101.462138],[3.131915,101.461706]],"type":"LineString"},"properties":{"extras":{"roadaccessrestrictions":{"summary":[{"amount":94.31,"distance":4047.5,"value":0},{"amount":5.69,"distance":244.3,"value":4}],"values":[[0,6,4],[6,81,0]]}},"fare":0,"segments":[{"distance":4291.8,"duration":389.7,"steps":[{"distance":244.3,"duration":58.6,"instruction":"Head north","name":"-","type":11,"way_points":[0,6]},{"distance":1001.1,"duration":72.1,"instruction":"Turn left onto Jalan Setia Permai U13/40","name":"Jalan Setia Permai U13/40","type":0,"way_points":[6,26]},{"distance":417.5,"duration":33.4,"instruction":"Turn left onto Persiaran Setia Permai","name":"Persiaran Setia Permai","type":0,"way_points":[26,30]},{"distance":1420.8,"duration":94.7,"instruction":"Turn right","name":"-","type":1,"way_points":[30,49]},{"distance":560.9,"duration":51.6,"instruction":"Turn left onto Persiaran Setia Wawasan","name":"Persiaran Setia Wawasan","type":0,"way_points":[49,57]},{"distance":502,"duration":44.4,"instruction":"Turn right onto Persiaran Setia Dagang","name":"Persiaran Setia Dagang","type":1,"way_points":[57,71]},{"distance":27.2,"duration":6.5,"instruction":"Turn left","name":"-","type":0,"way_points":[71,73]},{"distance":32.8,"duration":7.9,"instruction":"Turn left","name":"-","type":0,"way_points":[73,75]},{"distance":28,"duration":6.7,"instruction":"Turn right","name":"-","type":1,"way_points":[75,76]},{"distance":28.4,"duration":6.8,"instruction":"Turn right","name":"-","type":1,"way_points":[76,78]},{"distance":28.9,"duration":6.9,"instruction":"Keep right","name":"-","type":13,"way_points":[78,81]},{"distance":0,"duration":0,"instruction":"Arrive at your destination, straight ahead","name":"-","type":10,"way_points":[81,81]}]}],"summary":{"distance":4291.8,"duration":389.7},"transfers":0,"warnings":[{"code":1,"message":"There may be restrictions on some roads"}],"way_points":[0,81]},"type":"Feature"}],"metadata":{"attribution":"openrouteservice.org | OpenStreetMap contributors","engine":{"build_date":"2023-07-09T01:31:50Z","graph_date":"2023-11-10T16:57:08Z","version":"7.1.0"},"query":{"coordinates":[[101.46336772591981,3.1298175408991913],[101.4576429,3.1094739]],"format":"geojson","language":"en","profile":"driving-car"},"service":"routing","timestamp":1699888800555},"type":"FeatureCollection"}

Since this is a FeatureCollection, which is 0 indexed, you should try changing the 1 to a zero

to retrieve the 'first' set of coordinates 101.454941,3.109381 in the List. The lat / lon might be reversed. Quirks of using FeatureCollection script. :cry:

Finally! I was able to retrieve the coordinates. I can't even begin to express my gratitude to all of you :grinning:. However, I do have a question: why is the "select list item" block used twice?

Because you have lists within a list

See

This will produce an error in app I believe since in AppInventor index starts at 1

Did you try it? I think it may work. :wink: You have the code so perhaps try with a 0. Yes AppInvenetor index does start at 1 but FeatureCollections start at zero.

Yes I did :slight_smile:

Thank you.:slightly_smiling_face: This means Adam will have to convert All the directions to a List to obtain the results of the FeatureCollection Coordinates.
because using a dictionary will not provide the first item in the FC coordinates.

1 Like

perhaps

Show us?

oh ye agnostics

Note that the waypoint numbers are 0-indexed whereas lists in App Inventor are 1-based, so you will need to add 1 to each waypoint index of interest to get the corresponding point in the coordinates array

Understood, but we have an AI2 dictionary (of "dictionaries" and "lists") so when you break it down the indexes will start at 1 ?

Okay, will remember that if ever using values of tag 'waypoints'. But the domain of AI2 list indices remains 1,2,3,... as used in a Select Item from List block, though you might have to adjust waypoint numbers to fit into that domain.