我有一個MySQL數據庫,它包含我的客戶名稱以及他們業務的相應經度/緯度。我有一些代碼是從互聯網派生出來的,並且調整爲在一個郵政編碼的半徑範圍內進行搜索以找到它們,但它不在附近的某些地方。有誰知道API會搜索半徑並只返回我的數據庫中的POI?我可以想出10個這樣做不起作用的原因,但我認爲它不會受到傷害。提前致謝!尋找我的興趣點在半徑內?
- EDIT-- 這是我的工作代碼。我的數據庫具有lat/lng的正確數據。這就像烏鴉蒼蠅一樣,所以我正在尋找一些更具體的東西。思考?再次感謝!
session_start();
$MyID = $_SESSION['UserID'];
$MyZip = $_SESSION['HomeZip'];
function getCityData(){
$MyZip = $GLOBALS['MyZip'];
$db = $GLOBALS['db'];
$Qry = $db->query("SELECT * FROM 02_driver_geodata WHERE zip = $MyZip");
$Res = $Qry->fetch(PDO::FETCH_ASSOC);
$MyLat = $Res['Lat'];
$MyLng = $Res['Lng'];
return array('Lat' => $MyLat, 'Lng' => $MyLng);
}
function getRestData($SearchRadius = 25, $CreateDate = 'ALL'){
$db = $GLOBALS['db'];
//$R = 6371; // earth's mean radius, km
$R = 3959; //earth's mean radius, miles
$Specs = getCityData();
$lat = $Specs['Lat'];
$lon = $Specs['Lng'];
$rad = $SearchRadius;
// first-cut bounding box (in degrees)
$maxLat = $lat + rad2deg($rad/$R);
$minLat = $lat - rad2deg($rad/$R);
// compensate for degrees longitude getting smaller with increasing latitude
$maxLon = $lon + rad2deg($rad/$R/cos(deg2rad($lat)));
$minLon = $lon - rad2deg($rad/$R/cos(deg2rad($lat)));
$sql = "Select *, (acos(sin(:lat)*sin(radians(Lat)) + cos(:lat)*cos(radians(Lat))*cos(radians(Lng)-:lon)) * :R) As D
From 04_rest_00_locations
Where (acos(sin(:lat)*sin(radians(Lat)) + cos(:lat)*cos(radians(Lat))*cos(radians(Lng)-:lon)) * :R) < :rad
AND Lat Between :minLat And :maxLat
AND Lng Between :minLon And :maxLon
AND CreateDate > 01/01/2015 AND CreateDate <= NOW()
Order by D";
$params = array(
'lat' => deg2rad($lat),
'lon' => deg2rad($lon),
'minLat' => $minLat,
'minLon' => $minLon,
'maxLat' => $maxLat,
'maxLon' => $maxLon,
'rad' => $rad,
'R' => $R,
);
$points = $db->prepare($sql);
$points->execute($params);
$Return['Data'] = $points->fetchAll(PDO::FETCH_ASSOC);
$Return['Count'] = $points->rowCount();
return $Return;
}
您需要將每個郵政編碼與(緯度,經度)對關聯。完成之後,您可以嘗試使用Haversine公式來查找每個郵政編碼和每個業務之間的距離。請在此閱讀有關Haversine公式的更多信息:http://en.wikipedia.org/wiki/Haversine_formula – 2015-03-03 03:45:01
謝謝。我已經這樣做了。它實際上工作,我有lats/lngs,但它是像烏鴉飛,而不是確切的里程。這就是爲什麼我暫時在尋找一個API。謝謝! – b3tac0d3 2015-03-03 04:31:18