我有一个存储长和坐标的数据库表。用户键入他们的邮政编码,并应选择一个范围,例如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);
?>
你不应该真的'SELECT *'。你应该选择你想要的列。 – kkuilla 2015-02-10 12:27:07
听起来像你需要Haversine公式 - http://stackoverflow.com/questions/14750275/haversine-formula-with-php – martincarlin87 2015-02-10 12:33:10