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

Dear seniors

I want these sets of coordinate to become an input for some other procedure.


However, I have no idea on how I can extract them given the blocks that are already
available.

After you use the web block to convert the JSON to a dictionary, this is the path to that subdictionary:

features
1
geometry

1 Like

GotDirections(directions,points,distance,duration) is the eEvent indicating that a request has finished and has returned data. The following parameters are provided:

  • directions: A list of text directions, such as “Turn left at Massachusetts Avenue”.
  • points: A list of (latitude, longitude) points that represent the path to take. This can be passed to [LineString’s Points] (Maps) to draw the line on a [Map]
    (Maps).
  • distance: Estimated distance for the route, in meters.
  • duration: Estimated duration for the route, in seconds.

If that is not helpful try

ResponseContent the raw response from the server. This can be used to access more details beyond what the GotDirections event provides.

and read How do I achieve turn by turn navigation on AI? - #8 by ewpatton

Im terribly sorry for the late reply. I still have a question. Can I extract the value of the latitude and the longitude separately? Im sorry if I seem slow. I don't have any experience in coding at all. I plan on using these coordinates as input to calculate bearing.

extract them from what? A list of lat,lon perhaps? 3.10066, 101.72550 (an item in a list of lat,long) in Malaysia can become separated into lat 3.10066 and lon 101.72550

splitLatLon

What you do to separate depends on what form the latitude and longitude are in and what you intend to do with them when you get them. There are many other ways you can separate the latitude and longitude.

bearing:

Yes! Thank you, this was exactly what I was trying to figure out. I will continue to implement the method you provided and see how it goes. I will update if any new problems occur.

May I ask? What is the path to get the first value of coordinates. I tried to using the "select item index" block but the length of the list is actually 1 as it is under the key coordinates. Thank you in advance!

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.