3

I know two latlng points and also can get distance between 2 points. But how do I can get a latlng point on 1/3 of the distance of 2 points. latlng1 = [37.7052918, -8.8420179] latlng2 = [37.7062884, -8.8425087]

var point1 = new OpenLayers.Geometry.Point(latlng1.lon, latlng1.lat);
var point2 = new OpenLayers.Geometry.Point(latlng2.lon, latlng2.lat);

var line = new OpenLayers.Geometry.LineString([p1, p2]);
alert(line.getGeodesicLength(new OpenLayers.Projection("EPSG:4326")));
3

Here's how to do what you want with my npm package geographiclib. You can install this with:

npm install geographiclib

Then in node, do

var GeographicLib = require("geographiclib");
var Geodesic = GeographicLib.Geodesic,
    geod = Geodesic.WGS84;
var l = geod.InverseLine(37.7052918, -8.8420179, 37.7062884, -8.8425087),
    p = l.Position(l.s13/3);
console.log(p.lat2.toFixed(8) + " " + p.lon2.toFixed(8));

This prints out the point 1/3 of the way along the geodesic (from point 1 to point 2):

37.70562400 -8.84218150

The package is documented here. Look particularly at the example describing how to compute waypoints.

  • thanks but I found a better solution without any package. – Hu Kenneth Apr 11 '17 at 2:41
2

If you're using OpenLayers 3, you may need to use a mix of Vincenty (not in the standard build) and Ellipsoid. Ellipsoid contains

  • vincentyDistance
  • vincentyInitialBearing
  • vincentyFinalBearing

So, given two coordinates, you can get the distance, but you also need the initial bearing/azimuth because you'll need to call Util.DestinationVincenty (pass in 1 coordinate, bearing/azimuth, and 1/3 distance) to get the destination or target coordinate.

Someone more familiar with OpenLayers will probably have a better solution. I cobbled this one together via Google searches.

Note: Geodesy has direct and indirect problems. The direct, or first, problem is: given a coordinate, azimuth and distance, find the target coordinate. The indirect, or second, problem is: given 2 coordinates, find the distance and azimuth between them. Most algorithms for the indirect problem, like Vincenty's, can give you distance and azimuth but not all implementers include the azimuth. The problem is that it's required for the direct problem, like when you want to find points on a geodesic line.

  • thanks but I found a better solution without any package. – Hu Kenneth Apr 11 '17 at 2:41
2

I found a better solution.

http://www.movable-type.co.uk/scripts/latlong.html

LatLon.prototype.destinationPoint = function(distance, bearing, radius) {
    radius = (radius === undefined) ? 6371e3 : Number(radius);

    var delta = Number(distance) / radius;
    var Theta = Number(bearing).toRadians();

    var Phi1 = this.lat.toRadians();
    var Lambda1 = this.lon.toRadians();

    var sinPhi1 = Math.sin(Phi1), cosPhi1 = Math.cos(Phi1);
    var sindelta = Math.sin(delta), cosdelta = Math.cos(delta);
    var sinTheta = Math.sin(Theta), cosTheta = Math.cos(Theta);

    var sinPhi2 = sinPhi1 * cosdelta + cosPhi1 * sindelta * cosTheta;
    var Phi2 = Math.asin(sinPhi2);
    var y = sinTheta * sindelta * cosPhi1;
    var x = cosdelta - sinPhi1 * sinPhi2;
    var Lambda2 = Lambda1 + Math.atan2(y, x);

    return new LatLon(Phi2.toDegrees(), (Lambda2.toDegrees()+540)%360-180);         
   // normalise to −180..+180°
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.