[英]How can I convert latitude & longitude into north,south,east,west. if its possible In javascript?
let latitude: = 36.6839559;
let longitude = 3.6217802;
此 API 需要北、南、东、西参数
const API = `http://api.geonames.org/citiesJSON?north=${north}south=${south}&east=${east}&west=${west}&lang=de&username=demo`;
如果longitude < 0
则为West
,否则为East
。
如果latitude < 0
则为South
,否则为North
。
好吧,这个 api 似乎使用了边界框。 北,东,南和西是盒子的角落。 与其他答案相反,仅输出坐标是行不通的。 您需要所有的边界坐标。
因此,要将坐标转换为北、东等,我们应该加或减一公里(这已经足够近了)。 这是在 function kmInDegree(lat,long)
中计算的。 计算是根据此处找到的函数完成的。
因此,如果我们取阿尔及利亚的 Beni Amrane (36.68395,3.6217802) 并添加和减去正确的值,我们会得到这个边界框:
如果我们然后将这些值传递给 url 我们得到以下 output
const API = 'http://api.geonames.org/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"
}
]
}
可以使用以下 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.org/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.org/citiesJSON?north=${box.north}&south=${box.south}&east=${box.east}&west=${box.west}&lang=de&username=demo`; console.log(API); }
希望对你有帮助,如果没有,请评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.