<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>距离计算 - SOSOMapAPI</title> <script charset="utf-8" src="http://map.soso.com/api/v2/main.js?libraries=geometry"></script> <script> var EARTH_RADIUS = 6378137.0; //单位M var PI = Math.PI; function getRad(d){ return d*PI/180.0; } /** * caculate the great circle distance * @param {Object} latlng1 * @param {Object} latlng2 */ function getGreatCircleDistance(latlng1,latlng2){ var radLat1 = getRad(latlng1.lat); var radLat2 = getRad(latlng2.lat); var a = radLat1 - radLat2; var b = getRad(latlng1.lng) - getRad(latlng2.lng); var s = 2*Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); s = s*EARTH_RADIUS; s = Math.round(s*10000)/10000.0; return s; } var init = function(){ var m1=new soso.maps.LatLng(39.907625,116.375831); var m2=new soso.maps.LatLng(39.924347,116.411880); document.getElementById('info').innerHTML=getGreatCircleDistance(m1,m2); document.getElementById('info').innerHTML+=' | ' + soso.maps.geometry.spherical.computeDistanceBetween(m1,m2); } </script> </head> <body onload="init()"> <div id="info"></div> </body> </html>
仅供参考