0
我正在嘗試做節點+地理東西來檢索給定經緯度的所有位置,長度和半徑爲 爲那個計算,我正在嘗試下面的東西,但它是結果在NaN。JavaScript(node.js)geo calc導致NaN
類似的代碼在php,python中工作正常,但無法弄清楚我在這裏做錯了什麼。
REF:http://www.scribd.com/doc/2569355/Geo-Distance-Search-with-MySQL
PS 1:使用的NodeJS該計算,因爲它需要計算在服務器端。 PS 2:即使變量名稱看起來像php變量,它是js。
$cat file.js
var log =console.log;
var cos=Math.cos;
var sin=Math.sin;
function rad2deg(x){ log("rad2deg:", x,180*x/Math.pi);return (180*x/Math.pi);}
function deg2rad(x){ log("deg2rad:",x,x*Math.pi/180);return (x*Math.pi/180);}
function getNearPoint($lat,$lng,$rad){
console.log("Debug1",$lat,$lng);
$R = 6371; // earth's radius, km
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
console.log("Debug2",$lat,$lng);
// compensate for degrees lnggitude getting smaller with increasing latitude
$maxlng = $lng + rad2deg($rad/$R/cos(deg2rad($lat)));
$minlng = $lng - rad2deg($rad/$R/cos(deg2rad($lat)));
console.log("Debug3",$lat,$lng);
// convert origin of filter circle to radians
$lat = deg2rad($lat);
$lng = deg2rad($lng);
console.log("Debug4",$lat,$lng);
//Build database query and do rest of the stuff
}
getNearPoint(19.1947659,72.8768400,50);
這裏是輸出。
$ node file.js
Debug1 19.1947659 72.87684
rad2deg: 0.007848061528802385 NaN
rad2deg: 0.007848061528802385 NaN
Debug2 19.1947659 72.87684
deg2rad: 19.1947659 NaN
rad2deg: NaN NaN
deg2rad: 19.1947659 NaN
rad2deg: NaN NaN
Debug3 19.1947659 72.87684
deg2rad: 19.1947659 NaN
deg2rad: 72.87684 NaN
Debug4 NaN NaN
哎呀!萬分感謝!固定!!! –