简体   繁体   中英

How to find the angle between two coordinatess in mapbox

I was working on mapbox and on the basis of my current latlong , a random distance of 226mt and a random degree of 179 degrees , I calculated another latlong as follows.

function findpos(){
    var ptt = new mapboxgl.LngLat(latitude,longitude);
    var ptt2 = destinationPoint(179.85, 0.226, ptt);

    return ptt2;
}


  Number.prototype.toRad = function() {
   return this * Math.PI / 180;
}

Number.prototype.toDeg = function() {
   return this * 180 / Math.PI;
}

function destinationPoint(brng, dist, pointA) {
   dist = dist / 6378.1;
   brng = brng.toRad();

   var lat1 = pointA.lat.toRad(), lon1 = pointA.lng.toRad();

   var lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
                        Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));

   var lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
                                Math.cos(lat1),
                                Math.cos(dist) - Math.sin(lat1) *
                                Math.sin(lat2));

   if (isNaN(lat2) || isNaN(lon2)) return null;
   console.log("des ",lon2,lat2);
   return new mapboxgl.LngLat(lat2.toDeg(), lon2.toDeg());
}

It was okay till this point.But Now I wanted to find the angle or bearing between two latlongs and found this

// Converts from degrees to radians.
function toRadians(degrees) {
  return degrees * Math.PI / 180;
};

// Converts from radians to degrees.
function toDegrees(radians) {
  return radians * 180 / Math.PI;
}


function bearing(startLat, startLng, destLat, destLng){
  startLat = toRadians(startLat);
  startLng = toRadians(startLng);
  destLat = toRadians(destLat);
  destLng = toRadians(destLng);

  y = Math.sin(destLng - startLng) * Math.cos(destLat);
  x = Math.cos(startLat) * Math.sin(destLat) -
        Math.sin(startLat) * Math.cos(destLat) * Math.cos(destLng - startLng);
  brng = Math.atan2(y, x);
  brng = toDegrees(brng);
  return (brng + 360) % 360;
//  return brng;
}  

It calculates a degree and gives a result. But when i decided to double check this function by putting the already calculated latlongs by the function findpos() above, i should have got the answer as 179.85 degrees , but instead I get the angle as 270 degree .

Which part of the code is correct? I have created a working example here , and have logged the values in the console. Can anyone help me out here please?

You are using the mapboxgl.LngLat() function. The function name already contains the order in which it expects you to pass longitude and latitude -- > first longitude then latitude. You are passing first latitude then longitude. This will definitely cause a problem.

Your code:

 function findpos(){ var ptt = new mapboxgl.LngLat(latitude,longitude); var ptt2 = destinationPoint(179.85, 0.226, ptt); return ptt2; }

Try this:

 function findpos(){ var ptt = new mapboxgl.LngLat(longitude,latitude); var ptt2 = destinationPoint(179.85, 0.226, ptt); return ptt2; }

Similar for this part of your code:

 return new mapboxgl.LngLat(lat2.toDeg(), lon2.toDeg());

Have a look on Turf documentation

It provide's two different methods. You can try:

http://turfjs.org/docs/#bearing

http://turfjs.org/docs/#rhumbBearing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM