Convert nominatim data

Hallo again to everybody,
I try to get from nominatim data, latitude and longitude to compare the distance between two object.
When I cal the nominatim I call it with ajax on this way:

			$.ajax({ type: "GET",   
					 url: "https://nominatim.openstreetmap.org/search?q="+street_to_find+"&format=json",   
					 async: true,
					 success : function(text)
					 {
						 response = text;
						 alert(JSON.stringify(response));
						 risposta = JSON.stringify(response);
						 alert(risposta);
					 }
			});

(the alert are to control the program)

in my case the complete request become:

https://nominatim.openstreetmap.org/search?q=viale+dell'ingegneria+perugia&format=json

and this is the data that I get

[{"place_id":249038748,"licence":"Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright","osm_type":"way","osm_id":702281107,"boundingbox":["43.1129387","43.1143331","12.3562089","12.3619221"],"lat":"43.1134662","lon":"12.359101","display_name":"Viale dell'Ingegneria, Santa Lucia, Perugia, Umbria, 06127, Italia","class":"highway","type":"tertiary","importance":0.50001}]

As you can see I receive data between square bracket and I can’t parse it.
Yes, I can remove the bracket, (is not possible to parse the [0] item) , but is not elegance.
How can I solve it?

Cheers,
Sergio

Yeah, that’s by intention. The returned data is a list of dictionaries. In your case the list includes only one entry.

Yes, but if I try to to parse json data, json.parse(text) nothing happen

Hmm,“nothing happens”? what’s your code at that place?

json.parse(text) should return an array/list. You should be able to iterate over the elements.

json.parse() expects a string, so maybe json.parse(risposta) would be the better choice?

this is the eart of the problem:

 success : function(text)
{
  response = text;
  alert(JSON.stringify(response));
  risposta = JSON.parse(text);
  alert(risposta.lat);
}

In alert stringify I can see data.
After that I do a parsing to get data in risposta and after then I whant to see the latitude, but an error occour and nothig happen, not even an blank message

That will not work at all!

json.parse() will return a list, so accessing the 1st (out of many) element has to be done by indexing

alert(risposta[0].lat), maybe even alert(risposta[0][‘lat’]) ?

but, as said; json.parse() expects text as input, maybe the parameter (called ‘text’) of the function isn’t string?

What does

success : function(response)
{
  alert(JSON.stringify(response));
  risposta = JSON.parse(JSON.stringify(response));
  alert(risposta[0].lat);
}

give you?

Can you try using the Debugger/Console of your Browser (which one?) and look for error messages?

Yes, I have do it.
If I use parse.stringify works, the latitude is available, but those instructions I haven’t found anywhere.

Well, I tried

curl 'https://nominatim.openstreetmap.org/search?q=Ottobrunn&format=json'

and could see that the response is already a string output

So, from my humble point of view, the code could be?

success : function(json_response)
{
  alert(json_response);
  found_objects = JSON.parse(json_response);
  alert(found_objects[0].lat);
}

No sure about that.

That has nothing to do with OSM or nominatim, it’s JavaScript

2 Likes

This is my string to search

"https://nominatim.openstreetmap.org/search?q="+via_da_cercare+"&format=json"

I have tried to search also

https://nominatim.openstreetmap.org/search?q=Perugia, viale dell'ingegneria&format=json

with + and space in url string, but at the end I have to stringify and parse the result because without stringify I can’t access to data. I have tried both ways

https://nominatim.openstreetmap.org/search?q=Perugia,+viale+dell'ingegneria&format=json

Always good to encode the URI

encodeURI()

encodeURIComponent

1 Like

it’ works.
When happen those problems I’m afraid because the solution is not simple.
How can you know to stringify and then parse the result???
Bah!!

Best regard and thanks
Sergio

You’re welcome!

What does your code now look like: just that others can see working code.

No, is impossible to see working that because is for ambulance emergency.
If an ambulance is on a way I can calculate the distance by a new emergency. If is nearest I can allarm it. Now I will found to calculate the distance not with an aereal distance, but with real treet distance. OSM do it?

I just meant to share that tiny piece of code function() …

Regarding aerial distance versus using streets: that’s the function of a routing engine (navi)

See … using “osm routing engine” @ DuckDuckGo

Hi Toni,
can we open a new thread about this new code?
I haven’t undestand how it’s working and I have some doubts!

Sure, we can.

Please do note that any services at openstreetmap.org are partly provided by volunteers, have no service level guarantees, and may stop working or be withdrawn without notice.

They are really not something you should rely on for critical applications such as ambulances!

You can talk to a commercial company that uses OSM data to offer equivalent services, like the geocoding and routing services you’re interested in, but with an uptime guarantee. (Or you can use the data to host your own instances, but I suspect that’s probably not realistic in this case.)

3 Likes