GIS

From Code Trash
Jump to: navigation, search

Get destination from current location with a bearing and distance

Draw a line from one latlng to a destination with only bearing and distance User google map for this.

// ----------------------------------------
// Calculate new Lat/Lng from original points
// on a distance and bearing (angle)
// ----------------------------------------
let llFromDistance = function(latitude, longitude, distance, bearing) {
// taken from: https://stackoverflow.com/a/46410871/13549	
// distance in KM, bearing in degrees

    const R 		= 6378.1; // Radius of the Earth
    const brng	= bearing * Math.PI / 180; // Convert bearing to radian
    let lat = latitude * Math.PI / 180;// Current coords to radians
    let lon = longitude * Math.PI / 180;

    // Do the math magic
    lat = Math.asin(Math.sin(lat) * Math.cos(distance / R) + Math.cos(lat) * Math.sin(distance / R) * Math.cos(brng));
    lon += Math.atan2(Math.sin(brng) * Math.sin(distance / R) * Math.cos(lat), Math.cos(distance/R)-Math.sin(lat)*Math.sin(lat));

    // Coords back to degrees and return
    return [ (lat  * 180 / Math.PI), (lon  * 180 / Math.PI) ];

}

let pointsOnMapCircle = function(latitude, longitude, distance, numPoints) {
	const points = [];
  for (let i=0.0; i <= 360; ) {
  	const bearing =  i;
    console.log(bearing, i);
  	const newPoints = llFromDistance(latitude, longitude, distance, bearing);
    points.push(newPoints);
    i = i+ 0.1;
  }
 return points;
}

// Generate 360 points to form a circle
const points = pointsOnMapCircle(11.45196240526411,78.41729529161046, 0.2, 8);

points.forEach((p) => {

        var pl = new google.maps.Polyline({
            path: [ {lat: 11.45196240526411, lng: 78.41729529161046}, {lat: p[0], lng: p[1]} ],
            geodesic: true,
            strokeColor: '#FF0000',
            strokeOpacity: 1.0,
            strokeWeight: 1
        })
        pl.setMap(map);

    console.log(p[1],p[0]);
});