仅显示用户选择范围内的结果
Only show results within range chosen by user
我有一个存储经纬度坐标的数据库 table。用户输入他们的邮政编码并且应该 select 一个范围,例如 5 英里,然后应该显示数据库中存储的 5 英里以内的所有坐标。我已经设法将用户键入的邮政编码转换为坐标,但我发现很难进行下一部分以仅显示所选英里内的结果。
<?php
$postcode = urlencode("$_POST[postcode]"); // post code to look up in this case status however can easily be retrieved from a database or a form post
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=true"; // the request URL you'll send to google to get back your XML feed
$xml = simplexml_load_file($request_url) or die("url not loading");// XML request
$status = $xml->status;// GET the request status as google's api can return several responses
if ($status=="OK") {
//request returned completed time to get lat / lang for storage
$lat = $xml->result->geometry->location->lat;
$long = $xml->result->geometry->location->lng;
}
echo "$lat,$long";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM location";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo " Hobby: " . $row["lat"]. " Location: " . $row["long"]. "<br>";
$lat1=$row["lat"];
echo $lat1;
}
} else {
echo "Sorry, there are no meetups yet you can create one here ";
}
mysqli_close($conn);
?>
如果您有 mySQL 数据库,请查看这些问题,它们处理的是相同的问题。
Mysql within distance query
https://gis.stackexchange.com/questions/31628/find-points-within-a-distance-using-mysql
您需要创建一个 SQL 语句,其中特定距离内的所有坐标都是结果。它可能看起来像:
SELECT *,
( 3959 * acos( cos( radians($lat) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians($lng) )
+ sin( radians($lat) )
* sin( radians( lat ) ) ) ) AS distance
FROM locations
HAVING distance < $miles
ORDER BY distance
LIMIT 0, 20
如果您有 postgres 数据库,则可以使用 postGIS 扩展。存在一个函数ST_DWithin,如果坐标在一定距离内则返回真。
我有一个存储经纬度坐标的数据库 table。用户输入他们的邮政编码并且应该 select 一个范围,例如 5 英里,然后应该显示数据库中存储的 5 英里以内的所有坐标。我已经设法将用户键入的邮政编码转换为坐标,但我发现很难进行下一部分以仅显示所选英里内的结果。
<?php
$postcode = urlencode("$_POST[postcode]"); // post code to look up in this case status however can easily be retrieved from a database or a form post
$request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$postcode."&sensor=true"; // the request URL you'll send to google to get back your XML feed
$xml = simplexml_load_file($request_url) or die("url not loading");// XML request
$status = $xml->status;// GET the request status as google's api can return several responses
if ($status=="OK") {
//request returned completed time to get lat / lang for storage
$lat = $xml->result->geometry->location->lat;
$long = $xml->result->geometry->location->lng;
}
echo "$lat,$long";
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "test";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM location";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo " Hobby: " . $row["lat"]. " Location: " . $row["long"]. "<br>";
$lat1=$row["lat"];
echo $lat1;
}
} else {
echo "Sorry, there are no meetups yet you can create one here ";
}
mysqli_close($conn);
?>
如果您有 mySQL 数据库,请查看这些问题,它们处理的是相同的问题。
Mysql within distance query
https://gis.stackexchange.com/questions/31628/find-points-within-a-distance-using-mysql
您需要创建一个 SQL 语句,其中特定距离内的所有坐标都是结果。它可能看起来像:
SELECT *,
( 3959 * acos( cos( radians($lat) )
* cos( radians( lat ) )
* cos( radians( lng ) - radians($lng) )
+ sin( radians($lat) )
* sin( radians( lat ) ) ) ) AS distance
FROM locations
HAVING distance < $miles
ORDER BY distance
LIMIT 0, 20
如果您有 postgres 数据库,则可以使用 postGIS 扩展。存在一个函数ST_DWithin,如果坐标在一定距离内则返回真。