How to generate coordinates in between two known points

前端 未结 3 1953
攒了一身酷
攒了一身酷 2021-01-05 08:15

Background:

I\'m working with transport routes and Google provides Route points far apart enough to create \'shapes\'. These are the bus/train route

3条回答
  •  滥情空心
    2021-01-05 08:39

    Step 1 - Get the overall distance

    Comprehensive answer can be found here: http://www.movable-type.co.uk/scripts/latlong.html

    TL;DR:

    This uses the ‘haversine’ formula to calculate the great-circle distance between two points – that is, the shortest distance over the earth’s surface – giving an ‘as-the-crow-flies’ distance between the points (ignoring any hills, of course!).

    var R = 6371; // km
    var dLat = (lat2-lat1).toRad();
    var dLon = (lon2-lon1).toRad();
    var lat1 = lat1.toRad();
    var lat2 = lat2.toRad();
    
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
    var distance = R * c;
    

    Step 2 - Get the percentage travelled.

    Now you have the distance for this straight line, you can then work out a percentage of the overall distance for each 5 meters.

    Step 3 - Apply the percentage travelled to the difference between the Latitude and Longitude

    Find out the difference between the starting latitude and the final latitude. With this number, multiply it by the percentage traveled (as a decimal). This can then be added back to the starting latitude to find the current latitude of this point. Repeat for longitude.

提交回复
热议问题