我的代码未根据当前位置显示列表。它根据数据库顺序显示清单。
我想根据当前位置显示列表。因此,我将首先从数据库(mysqli)获取最近的列表,然后再从数据库(mysqli)获取最近列表,然后继续...
我正在使用getlocation函数获取当前经度($ long)和纬度($ lat)。并从数据库中获取经度和纬度。并将其传递给Haversine公式。
我没有得到最接近当前位置的准确列表位置。 请帮助我。
<?php include_once('header.php');
if(empty($_REQUEST['getlocation'])){
$lat=$_REQUEST['lat'];
$long=$_REQUEST['long'];
}else{
echo"unable to Get location";
}
?>
<script src="https://use.fontawesome.com/13118c3275.js"></script>
<script>
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(redirectToPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function redirectToPosition(position) {
window.location='testing123.php?lat='+position.coords.latitude+'&long='+position.coords.longitude;
}
</script>
<div id="locationbox">
<span><button onclick="getLocation()" name="getlocation" id="location" >Find my near stores</button></span><span><img src="image/location.png" style="width: 25px; margin-left: 3px;" /></span>
</div>
<div id="mainbox">
<h2 style="text-align:center;">Online Service</h2>
<ol type='A'>
<?php
require_once("db.php");
echo $q="SELECT * FROM `local_business` where maincategory='Local Business'";
$result0 = $link->query($q);
$link->close();
if ($result0->num_rows > 0) {
while($row = $result0->fetch_assoc()) {
$myArray[] = $row;
echo "<br>";
$datalat=$row['latitude'];
$datalong=$row['longitude'];
echo "<div class='img_div' id=".$row['id'].">";
echo"<img id='img-style' src='images/".$row['lbimage']."'>";
echo"<li class='p-style' id='title-style'>".$row['lbname']."</li>";
echo "<br>";
echo"<p class='p-style'>".$row['lbcategory']."</p>";
echo "<br>";
echo"<p class='p-style' id='businessaddress' >Address: ".$row['lbaddress']."</p>";
echo"<p class='p-style' id='phone'>phone: ".$row['phone']."</p>";
echo"<p class='p-style' id='details'>Details: ".$row['lbdetails']."</p>";
echo"<button type='button' class='applybutton'><a href=http://".$row['lbwebsite']." target=_blank>Visit website</a></button>";
echo"</div>";
}
$mic=json_encode($myArray);
$mic = preg_replace('/\s*\"\s*/',"'",$mic);
}
echo $sql = "SELECT where maincategory='Local Business', ( 6371 * acos( cos( radians(" . $lat . ") ) * cos( radians(" . $datalat . ") ) * cos( radians(" . $datalong . ") - radians(" . $long . ") ) + sin( radians(" . $lat . ") ) * sin( radians(" . $datalat . ") ) ) ) AS distance FROM `local_business` HAVING distance < 5 ";
?>
</ol>
</div>
<!-------------map----------->
<script type='text/javascript' src='jquery-1.6.2.min.js'></script>
<script type='text/javascript' src='jquery-ui-1.8.14.custom.min.js'></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBrO2tjqXICh2bqdCJnX9FXyWMAbfeJZlY"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var markers = <?php echo $mic; ?>;
window.onload = function () {
LoadMap();
}
function LoadMap() {
var mapOptions = {
center: new google.maps.LatLng(markers[0].latitude, markers[0].longitude),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var infoWindow = new google.maps.InfoWindow();
var latlngbounds = new google.maps.LatLngBounds();
var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions);
var legend = document.getElementById("legend");
legend.innerHTML = "";
var useAplhabets = document.getElementById("alphabet").checked;
var start_letter_code = useAplhabets ? 97 : 1;
var marker_color = "009BEE";
var marker_text_color = "FFFFFF";
for (var i = 0; i < markers.length; i++) {
var character = useAplhabets ? String.fromCharCode(start_letter_code).toUpperCase() : start_letter_code;
start_letter_code++;
var data = markers[i]
var myLatlng = new google.maps.LatLng(data.latitude, data.longitude);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
lbname: data.lbname,
icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=" + character + "|" + marker_color + "|" + marker_text_color
});
(function (marker, data) {
google.maps.event.addListener(marker, "click", function (e) {
infoWindow.setContent("<div style = 'width:100px;height:40px'>" + marker.lbname + " " + data.lbdetails + "</div>");
infoWindow.open(map, marker);
});
})(marker, data);
latlngbounds.extend(marker.position);
legend.innerHTML += "<div style = 'margin:5px'><img align = 'middle' src = '" + marker.icon + "' /> " + marker.lbname + "</div>";
}
var bounds = new google.maps.LatLngBounds();
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
}
</script>
<div>
<label for="alphabet">
Alphabets
<input type="radio" id="alphabet" name="letter_number" value="1" checked="checked"
onclick="LoadMap()" /></label>
<label for="number">
Numbers
<input type="radio" id="number" name="letter_number" value="2" onclick="LoadMap()" /></label>
</div>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<div id="dvMap" style="width: 365px; height: 300px; border: 2px solid #000; ">
</div>
</td>
<td id="legend">
</td>
</tr>
</table>
<!------------------------map--------------->
<?php include_once('footer.php'); ?>