0

此代码计算大圆距离。原始来源在这里:http ://www.movable-type.co.uk/scripts/latlong-vincenty.html

我确定它缺少一些简单的东西,但是 javascript 不会执行。

http://jsfiddle.net/RvdJH/

<script type="text/javascript">

function distVincenty(lat1, lon1, lat2, lon2) {
    var a = 6378137,
        b = 6356752.314245,
        f = 1 / 298.257223563; // WGS-84 ellipsoid params
    var L = toRad(long2 -lon1);
     function toRad(degrees){
         return degrees * Math.PI / 180;
     }
    var U1 = Math.atan((1 - f) * Math.tan(lat1.toRad()));
    var U2 = Math.atan((1 - f) * Math.tan(lat2.toRad()));
    var sinU1 = Math.sin(U1),
        cosU1 = Math.cos(U1);
    var sinU2 = Math.sin(U2),
        cosU2 = Math.cos(U2);

    var lambda = L,
        lambdaP, iterLimit = 100;
    do {
        var sinLambda = Math.sin(lambda),
            cosLambda = Math.cos(lambda);
        var sinSigma = Math.sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda) + (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda) * (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
        if (sinSigma == 0) return 0; // co-incident points
        var cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
        var sigma = Math.atan2(sinSigma, cosSigma);
        var sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
        var cosSqAlpha = 1 - sinAlpha * sinAlpha;
        var cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
        if (isNaN(cos2SigmaM)) cos2SigmaM = 0; // equatorial line: cosSqAlpha=0 (§6)
        var C = f / 16 * cosSqAlpha * (4 + f * (4 - 3 * cosSqAlpha));
        lambdaP = lambda;
        lambda = L + (1 - C) * f * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
    } while (Math.abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0);

    if (iterLimit == 0) return NaN // formula failed to converge
    var uSq = cosSqAlpha * (a * a - b * b) / (b * b);
    var A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
    var B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
    var deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
    var s = b * A * (sigma - deltaSigma);

    s = s.toFixed(3); // round to 1mm precision
    return s;

    // note: to return initial/final bearings in addition to distance, use something like:
    var fwdAz = Math.atan2(cosU2 * sinLambda, cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
    var revAz = Math.atan2(cosU1 * sinLambda, -sinU1 * cosU2 + cosU1 * sinU2 * cosLambda);
    return {
        distance: s,
        initialBearing: fwdAz.toDeg(),
        finalBearing: revAz.toDeg()
    };
}​

</script>


<table>
    <tr>
        <td>
            <p>
                Lat 1: <input name="lat1" value="53.0902"> 
                Long 1: <input name="lon1" value="001.5040">
            </p>
            <p>
                Lat 2: <input name="lat2" value="52.1219"> 
                Long 2: <input name="lon2" value="040.0833">
            </p>
            <input type="button" value="calculate distance" onClick="document.getElementsByName('result')[0].value = distVincenty( document.getElementsByName('lat1')[0].value ,document.getElementsByName('lon1')[0].value , document.getElementsByName('lat2')[0].value, document.getElementsByName('lon2')[0].value);">
        </td>
    </tr>
</table>


</body>
4

1 回答 1

0

您不能访问像 result.value 这样的输入值。使用喜欢

document.getElementsByName('result')[0].value

并且代码获取 toRad is not defined 错误。添加此方法

Number.prototype.toRad = function() {  // convert degrees to radians
 return this * Math.PI / 180;
}
Number.prototype.toDeg = function() {  // convert radians to degrees
  return this * 180 / Math.PI;
}
于 2012-04-15T08:07:40.477 回答