let latitude: = 36.6839559;
let longitude = 3.6217802;
This API Requires north, south, east, west Parameters
const API = `=${north}south=${south}&east=${east}&west=${west}&lang=de&username=demo`;
let latitude: = 36.6839559;
let longitude = 3.6217802;
This API Requires north, south, east, west Parameters
const API = `http://api.geonames/citiesJSON?north=${north}south=${south}&east=${east}&west=${west}&lang=de&username=demo`;
Share
Improve this question
edited Jul 6, 2020 at 14:15
Hakem Ryan
asked Jul 3, 2020 at 17:16
Hakem RyanHakem Ryan
311 silver badge6 bronze badges
0
2 Answers
Reset to default 4If longitude < 0
then West
, else East
.
If latitude < 0
then South
, else North
.
Well, it seems like this api uses bounding box. With north, east, south and west being the corners of the box. And contrary to the other answers, just outputting the coordinates won't work. You need all of the bounding coordinates.
So to convert the coordinates to north, east etc we should add or subtract a kilometer (This is close enough). That is calculated in the function kmInDegree(lat,long)
. The calculations are done based on the functions found here .
So if we take the Beni Amrane, Algeria (36.68395,3.6217802) and add and subtract the right values we get this bounding box:
If we then pass those values to the url we get the following output
const API = 'http://api.geonames/citiesJSON?north=36.69395&south=36.67395&east=3.6324802&west=3.6104802&lang=de&username=demo';
//Result of the api call above
{
"geonames": [
{
"lng": 3.616667,
"geonameId": 2507983,
"countrycode": "DZ",
"name": "’Aïn N’Sara",
"fclName": "city, village,...",
"toponymName": "’Aïn N’Sara",
"fcodeName": "populated place",
"wikipedia": "",
"lat": 36.683333,
"fcl": "P",
"population": 0,
"fcode": "PPL"
}
]
}
One can automate this process with the following function:
function getBox(lat, long) {
//calculate the offset of 1km at a certain coordinate
const dist = kmInDegree(lat, long)
//calculate the bounds and make an object of them
let bounds = {
north: lat + dist.lat,
south: lat - dist.lat,
east: long + dist.long,
west: long - dist.long
};
return bounds;
}
//See https://en.wikipedia/wiki/Latitude#Length_of_a_degree_of_latitude
function kmInDegree(lat, long) {
const pi = Math.PI;
const eSq = 0.00669437999014;
const a = 6378137.0; //equatorial radius in metres
lat = lat * pi / 180; //convert to radians
long = long * pi / 180; //convert radians
//I won't try to explain the calculations. All i know is that they are correct with the examples on wikipedia (see url above)
const latLength = (pi * a * (1 - eSq)) / (180 * Math.pow((1 - eSq * Math.pow(Math.sin(lat), 2)), 3 / 2));
const longLength = (pi * a * Math.cos(long)) / (180 * Math.sqrt((1 - (eSq * Math.pow(Math.sin(long), 2)))));
//If you want a greater offset, say 5km then change 1000 into 5000
return {
lat: 1000 / latLength,
long: 1000 / longLength
};
}
//Just for demonstration purposes only.
window.onload = function() {
//get the box from the coordinates
let box = getBox(36.68395, 3.6217802);
//create the API URL
const API = `http:\/\/api.geonames/citiesJSON?north=${box.north}&south=${box.south}&east=${box.east}&west=${box.west}&lang=de&username=demo`;
console.log(API);
}
Hope this helps! If not, please ment