admin管理员组文章数量:1022752
Let's say I have the latitude and longitude coordinate [30, -87]. From that point, I would like to be able to find where my location would be after traveling let's say 10km at 45 degrees of bearing.
My first attempt at trying to solve this didn't go so well as you can see below. This obviously doesn't account for the units which is really the hardest part to prehend for me at least.
var bearing = 45;
var distance = 10;
var position = {
"latitude": 30,
"longitude": -87
};
var newLat = Math.cos(bearing) * distance + position.latitude;
var newLon = Math.sin(bearing) * distance + position.longitude;
I'm assuming the radius of the earth will e into play at one point but I really didn't know where to start so any help would be greatly appreciated.
Let's say I have the latitude and longitude coordinate [30, -87]. From that point, I would like to be able to find where my location would be after traveling let's say 10km at 45 degrees of bearing.
My first attempt at trying to solve this didn't go so well as you can see below. This obviously doesn't account for the units which is really the hardest part to prehend for me at least.
var bearing = 45;
var distance = 10;
var position = {
"latitude": 30,
"longitude": -87
};
var newLat = Math.cos(bearing) * distance + position.latitude;
var newLon = Math.sin(bearing) * distance + position.longitude;
I'm assuming the radius of the earth will e into play at one point but I really didn't know where to start so any help would be greatly appreciated.
Share Improve this question asked Aug 28, 2021 at 5:12 lmg1114lmg1114 1932 silver badges7 bronze badges 2- It's an integration problem. You can approximate the earth to a sphere and use the spherical coordinates. cos(bearing) gives change along azimuthal angle. sin(bearing) gives change along polar angle. I feel someone at math stack exchange could give a more detailed explanation than one you'd get on stackoverflow – NoShady420 Commented Aug 28, 2021 at 5:44
- or gis. There are a lot of links to various levels of plexity of explanation, and they use different programming languages in discussion. gis.stackexchange./questions/5821/… – Chris Strickland Commented Aug 28, 2021 at 5:58
3 Answers
Reset to default 6I have used python code here: Get lat/long given current point, distance and bearing, and converted in into JS code to get the answer.
Here is the code:
const bearing = 45;
const bearing_rad = (bearing*Math.PI)/180;
const distance = 10;
const EARTH_RADIUS = 6378.137;
const initial_position = {
"latitude": 30,
"longitude": -87
};
const init_lat = (initial_position.latitude*Math.PI)/180;
const init_lon = (initial_position.longitude*Math.PI)/180;
const final_lat = (180/Math.PI)*(Math.asin( Math.sin(init_lat)*Math.cos(distance/EARTH_RADIUS) + Math.cos(init_lat)*Math.sin(distance/EARTH_RADIUS)*Math.cos(bearing_rad)));
const final_lon = (180/Math.PI)*(init_lon + Math.atan2(Math.sin(bearing_rad)*Math.sin(distance/EARTH_RADIUS)*Math.cos(init_lat), Math.cos(distance/EARTH_RADIUS)-Math.sin(init_lat)*Math.sin(final_lat)));
console.log(final_lat, final_lon); // 30.06350049619102 -86.96303358059173
Right code of js:
const bearing = 45;
const bearing_rad = (bearing*Math.PI)/180;
const distance = 10;
const EARTH_RADIUS = 6378.137;
const initial_position = {
"latitude": 30,
"longitude": -87
};
const init_lat = (initial_position.latitude*Math.PI)/180;
const init_lon = (initial_position.longitude*Math.PI)/180;
const radian_lat = (Math.asin( Math.sin(init_lat)*Math.cos(distance/EARTH_RADIUS) + Math.cos(init_lat)*Math.sin(distance/EARTH_RADIUS)*Math.cos(bearing_rad)));
const final_lon = (180/Math.PI)*(init_lon + Math.atan2(Math.sin(bearing_rad)*Math.sin(distance/EARTH_RADIUS)*Math.cos(init_lat), Math.cos(distance/EARTH_RADIUS)-Math.sin(init_lat)*Math.sin(radian_lat)));
const final_lat = (180/Math.PI)*radian_lat
console.log(final_lat, final_lon); // 30.06350049619102 -86.96303358059173
The accepted answer is slightly incorrect. Where final_lat is included in the final_lon calculation it should be in radians, but as written it is in degrees. The code below is from user25354956. I don’t currently have the reputation to write a ment.
const bearing = 45;
const bearing_rad = (bearing*Math.PI)/180;
const distance = 10;
const EARTH_RADIUS = 6378.137;
const initial_position = {
"latitude": 30,
"longitude": -87
};
const init_lat = (initial_position.latitude*Math.PI)/180;
const init_lon = (initial_position.longitude*Math.PI)/180;
const radian_lat = (Math.asin( Math.sin(init_lat)*Math.cos(distance/EARTH_RADIUS) + Math.cos(init_lat)*Math.sin(distance/EARTH_RADIUS)*Math.cos(bearing_rad)));
const final_lon = (180/Math.PI)*(init_lon + Math.atan2(Math.sin(bearing_rad)*Math.sin(distance/EARTH_RADIUS)*Math.cos(init_lat), Math.cos(distance/EARTH_RADIUS)-Math.sin(init_lat)*Math.sin(radian_lat)));
const final_lat = (180/Math.PI)*radian_lat
console.log(final_lat, final_lon); // 30.06350049619102 -86.96303358059173
Let's say I have the latitude and longitude coordinate [30, -87]. From that point, I would like to be able to find where my location would be after traveling let's say 10km at 45 degrees of bearing.
My first attempt at trying to solve this didn't go so well as you can see below. This obviously doesn't account for the units which is really the hardest part to prehend for me at least.
var bearing = 45;
var distance = 10;
var position = {
"latitude": 30,
"longitude": -87
};
var newLat = Math.cos(bearing) * distance + position.latitude;
var newLon = Math.sin(bearing) * distance + position.longitude;
I'm assuming the radius of the earth will e into play at one point but I really didn't know where to start so any help would be greatly appreciated.
Let's say I have the latitude and longitude coordinate [30, -87]. From that point, I would like to be able to find where my location would be after traveling let's say 10km at 45 degrees of bearing.
My first attempt at trying to solve this didn't go so well as you can see below. This obviously doesn't account for the units which is really the hardest part to prehend for me at least.
var bearing = 45;
var distance = 10;
var position = {
"latitude": 30,
"longitude": -87
};
var newLat = Math.cos(bearing) * distance + position.latitude;
var newLon = Math.sin(bearing) * distance + position.longitude;
I'm assuming the radius of the earth will e into play at one point but I really didn't know where to start so any help would be greatly appreciated.
Share Improve this question asked Aug 28, 2021 at 5:12 lmg1114lmg1114 1932 silver badges7 bronze badges 2- It's an integration problem. You can approximate the earth to a sphere and use the spherical coordinates. cos(bearing) gives change along azimuthal angle. sin(bearing) gives change along polar angle. I feel someone at math stack exchange could give a more detailed explanation than one you'd get on stackoverflow – NoShady420 Commented Aug 28, 2021 at 5:44
- or gis. There are a lot of links to various levels of plexity of explanation, and they use different programming languages in discussion. gis.stackexchange./questions/5821/… – Chris Strickland Commented Aug 28, 2021 at 5:58
3 Answers
Reset to default 6I have used python code here: Get lat/long given current point, distance and bearing, and converted in into JS code to get the answer.
Here is the code:
const bearing = 45;
const bearing_rad = (bearing*Math.PI)/180;
const distance = 10;
const EARTH_RADIUS = 6378.137;
const initial_position = {
"latitude": 30,
"longitude": -87
};
const init_lat = (initial_position.latitude*Math.PI)/180;
const init_lon = (initial_position.longitude*Math.PI)/180;
const final_lat = (180/Math.PI)*(Math.asin( Math.sin(init_lat)*Math.cos(distance/EARTH_RADIUS) + Math.cos(init_lat)*Math.sin(distance/EARTH_RADIUS)*Math.cos(bearing_rad)));
const final_lon = (180/Math.PI)*(init_lon + Math.atan2(Math.sin(bearing_rad)*Math.sin(distance/EARTH_RADIUS)*Math.cos(init_lat), Math.cos(distance/EARTH_RADIUS)-Math.sin(init_lat)*Math.sin(final_lat)));
console.log(final_lat, final_lon); // 30.06350049619102 -86.96303358059173
Right code of js:
const bearing = 45;
const bearing_rad = (bearing*Math.PI)/180;
const distance = 10;
const EARTH_RADIUS = 6378.137;
const initial_position = {
"latitude": 30,
"longitude": -87
};
const init_lat = (initial_position.latitude*Math.PI)/180;
const init_lon = (initial_position.longitude*Math.PI)/180;
const radian_lat = (Math.asin( Math.sin(init_lat)*Math.cos(distance/EARTH_RADIUS) + Math.cos(init_lat)*Math.sin(distance/EARTH_RADIUS)*Math.cos(bearing_rad)));
const final_lon = (180/Math.PI)*(init_lon + Math.atan2(Math.sin(bearing_rad)*Math.sin(distance/EARTH_RADIUS)*Math.cos(init_lat), Math.cos(distance/EARTH_RADIUS)-Math.sin(init_lat)*Math.sin(radian_lat)));
const final_lat = (180/Math.PI)*radian_lat
console.log(final_lat, final_lon); // 30.06350049619102 -86.96303358059173
The accepted answer is slightly incorrect. Where final_lat is included in the final_lon calculation it should be in radians, but as written it is in degrees. The code below is from user25354956. I don’t currently have the reputation to write a ment.
const bearing = 45;
const bearing_rad = (bearing*Math.PI)/180;
const distance = 10;
const EARTH_RADIUS = 6378.137;
const initial_position = {
"latitude": 30,
"longitude": -87
};
const init_lat = (initial_position.latitude*Math.PI)/180;
const init_lon = (initial_position.longitude*Math.PI)/180;
const radian_lat = (Math.asin( Math.sin(init_lat)*Math.cos(distance/EARTH_RADIUS) + Math.cos(init_lat)*Math.sin(distance/EARTH_RADIUS)*Math.cos(bearing_rad)));
const final_lon = (180/Math.PI)*(init_lon + Math.atan2(Math.sin(bearing_rad)*Math.sin(distance/EARTH_RADIUS)*Math.cos(init_lat), Math.cos(distance/EARTH_RADIUS)-Math.sin(init_lat)*Math.sin(radian_lat)));
const final_lat = (180/Math.PI)*radian_lat
console.log(final_lat, final_lon); // 30.06350049619102 -86.96303358059173
本文标签:
版权声明:本文标题:javascript - How to get Latitude and Longitude after going a distance at certain bearing from another point? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745525969a2154525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论