我试图找到两点之间的距离,但每个纬度和经度我从数据库中获取基于的邮编
我试图将查询作为变量,但我不知道如何使这个想法最终起作用。
任务:我有10个商店,每个地址和邮政编码,然后我有1000个用户地址和邮政编码,我需要为每个用户找到3个最近的商店,理想情况下提取为电子表格,这样每个用户将是一个额外的3列将关闭商店。
我下载完整的邮政编码数据库,并尝试将用户/商店邮政编码与邮政编码数据库连接起来,获取每个用户/商店的经纬度,但是从这里我现在确定我需要做什么以及如何处理它进一步。
P.S。请不要宣传谷歌地图API,我知道,但对我来说太贵了。
任何有关此任务的建议或任何帮助都会让我们感激不尽。
谢谢。
这是我的PHP
代码:
$sqlTescLat = "SELECT lat FROM postcodelatlng, tesco WHERE tesco.postcode = postcodelatlng.postcode";
$sqlTescLng = "SELECT lng FROM postcodelatlng, tesco WHERE tesco.postcode = postcodelatlng.postcode";
$sqlShopLat = "SELECT lat FROM postcodelatlng, shops WHERE shops.postcode = postcodelatlng.postcode";
$sqlShopLng = "SELECT lng FROM postcodelatlng, shops WHERE shops.postcode = postcodelatlng.postcode";
$tescoLat = mysqli_query($con,$sqlTescLat);
$tescoLng = mysqli_query($con,$sqlTescLng);
$shopsLat = mysqli_query($con,$sqlShopLat);
$shopsLng = mysqli_query($con,$sqlShopLng);
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
return ($miles * 1.609344);
} else if ($unit == "N") {
return ($miles * 0.8684);
} else {
return $miles;
}
}
echo distance($tescoLat, $tescoLng, $shopsLat, $shopsLng, "M") . " Miles<br>";
这就是数据库的样子:
shops
ID NAME ADDRESS POSTCODE
1 Home1 63 Simmonds Close BH15 3EB
tesco
ID NAME ADDRESS POSTCODE
1 Tesco Poole Waterloo Rd, Poole BH17 7EJ, UK BH17 7EJ
postcodelatlng(this is big post codes database) as e.g.:
ID POSTCODE lat lng
1 AB10 1XG 57.144165160000000 -2.114847768000000
更新
$sqlDestLat = "SELECT lat FROM postcodelatlng, tesco WHERE tesco.postcode = postcodelatlng.postcode";
$sqlDestLng = "SELECT lng FROM postcodelatlng, tesco WHERE tesco.postcode = postcodelatlng.postcode";
$sqlOrigLat = "SELECT lat FROM postcodelatlng, shops WHERE shops.postcode = postcodelatlng.postcode";
$sqlOrigLng = "SELECT lng FROM postcodelatlng, shops WHERE shops.postcode = postcodelatlng.postcode";
$sqlDISTANCE="SELECT *, 3956 * 2 * ASIN(SQRT( POWER(SIN(('$sqlOrigLat' - abs('$sqlDestLat')) * pi()/180 / 2),2) + COS('$sqlOrigLat' * pi()/180 ) * COS(abs('$sqlDestLat') * pi()/180) * POWER(SIN(('$sqlOrigLng' – '$sqlDestLng') * pi()/180 / 2), 2) )) as distance FROM tesco, shops, postcodelatlng having distance < 20 BY distance limit 10;"