Massimo Scola
asked on
How to retrieve an array from within a JSON array? OpenstreetMap Nominatim Lookup
I would like to get the latitude and longitudes of an address from the OpenstreetMap Server. I use the following code:
The URI https://nominatim.openstreetmap.org/search/Keizersgracht%202,%20Amsterdam?format=json
gives me the following JSON output:
This is my code to get the data:
My code does not yet work as expected. As you can see, some addresses return more than boundingbox.
How do I get the first array item's bounding box with JavaScript so that I can return the lat/lng?
The URI https://nominatim.openstreetmap.org/search/Keizersgracht%202,%20Amsterdam?format=json
gives me the following JSON output:
[
{
"place_id": 29597926,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2729940028,
"boundingbox": [
"52.3654887",
"52.3655887",
"4.8878076",
"4.8879076"
],
"lat": "52.3655387",
"lon": "4.8878576",
"display_name": "489B-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1017DM, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29345477,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2722023917,
"boundingbox": [
"52.3669115",
"52.3670115",
"4.8842236",
"4.8843236"
],
"lat": "52.3669615",
"lon": "4.8842736",
"display_name": "460-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1016GE, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29416200,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2729939619,
"boundingbox": [
"52.3664731",
"52.3665731",
"4.8847569",
"4.8848569"
],
"lat": "52.3665231",
"lon": "4.8848069",
"display_name": "470-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1017EG, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29866709,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2737809901,
"boundingbox": [
"52.3635475",
"52.3636475",
"4.898591",
"4.898691"
],
"lat": "52.3635975",
"lon": "4.898641",
"display_name": "766-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1017EB, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29285837,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2722023414,
"boundingbox": [
"52.3674621",
"52.3675621",
"4.8847902",
"4.8848902"
],
"lat": "52.3675121",
"lon": "4.8848402",
"display_name": "405-2, Keizersgracht, Negen Straatjes, Amsterdam, Noord-Holland, Nederland, 1016EK, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29667468,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2729940808,
"boundingbox": [
"52.3647928",
"52.3648928",
"4.8890486",
"4.8891486"
],
"lat": "52.3648428",
"lon": "4.8890986",
"display_name": "523-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1017DP, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 30045384,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2737809134,
"boundingbox": [
"52.3639034",
"52.3640034",
"4.8977648",
"4.8978648"
],
"lat": "52.3639534",
"lon": "4.8978148",
"display_name": "701-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1017DW, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29337731,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2721355336,
"boundingbox": [
"52.3756178",
"52.3757178",
"4.8857254",
"4.8858254"
],
"lat": "52.3756678",
"lon": "4.8857754",
"display_name": "172A-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1015CZ, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29077463,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2721355435,
"boundingbox": [
"52.3791909",
"52.3792909",
"4.8890042",
"4.8891042"
],
"lat": "52.3792409",
"lon": "4.8890542",
"display_name": "18-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1015CP, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
},
{
"place_id": 29212365,
"licence": "Data © OpenStreetMap contributors, ODbL 1.0. https://osm.org/copyright",
"osm_type": "node",
"osm_id": 2721354960,
"boundingbox": [
"52.3754143",
"52.3755143",
"4.8864475",
"4.8865475"
],
"lat": "52.3754643",
"lon": "4.8864975",
"display_name": "155-2, Keizersgracht, Grachtengordel, Amsterdam, Noord-Holland, Nederland, 1015CL, Nederland",
"class": "place",
"type": "house",
"importance": 0.32100000000000006
}
]
This is my code to get the data:
/**
* @description Looks up the coordinates based on an address
* @returns {text} coordinates of an input address
*/
this.lookupCoordinates = function(anAddress) {
boundingBox: [];
var url = "http://nominatim.openstreetmap.org/search/" + encodeURI(anAddress) + "?format=json";
/* Get the data */
$.get(url, function (data) {
var obj = $.parseJSON(data); //
boundingBox = obj.data[0].boundingbox;
}
);
console.log(boundingBox);
};
My code does not yet work as expected. As you can see, some addresses return more than boundingbox.
How do I get the first array item's bounding box with JavaScript so that I can return the lat/lng?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER