[英]Display all locations from database within a given radius
使用谷歌地圖我想檢索並顯示具有給定點的固定半徑的所有位置。
我已經設法找到顯示位置指南,並且還看到了許多關於使用SQL查詢檢索它的帖子。 我的數據庫包含項目名稱,地址(查找alt,lon),alt,lon和description。 我如何使用存儲的alt,lon來僅檢索其中的半徑,比如半徑為50km。
這是我的代碼:
JavaScript的
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(31.046051, 34.85161199999993);
var myOptions = {
zoom: 7,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function updateCoordinates(latlng)
{
if(latlng)
{
document.getElementById('lat').value = latlng.lat();
document.getElementById('lng').value = latlng.lng();
}
}
function codeAddress() {
var address = document.getElementById("address").value;
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
updateCoordinates(results[0].geometry.location);
if (marker) marker.setMap(null);
marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
draggable: true
});
google.maps.event.addListener(marker, "dragend", function() {
updateCoordinates(marker.getPosition());
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
function showPositionCoupons(sentlat, sentlon)
{
lat=sentlat;
lon=sentlon;
latlon=new google.maps.LatLng(lat, lon)
mapholder=document.getElementById('map_canvas')
var myOptions={
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
};
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
marker = new google.maps.Marker({position:latlon,map:map,title:"You are here!"});
}
我想我需要在循環中使用showPositionCoupons()
,同時閱讀alt和lon? 感謝您提供的任何幫助,我知道這是一個經常出現的問題但我無法使用現有的方法來解決它。
我曾嘗試使用Display Location指南DisplayLocations()方法,但它對我來說不起作用,雖然位置在那里呈現的方式是完美的,只需要排除半徑范圍外的那些。
可能不完全是你所期待的,但我希望它可能會有所幫助。 從你發布的鏈接開始,我帶你使用PHP / MySQL。
如果這是真的,我會使用PHP / MySQL來獲取正確的結果,然后在Google地圖上顯示它們。
如果您可以在不需要外部服務的情況下進行計算,效率會更高。
// PHP/MySQL code
$sourceLat = '';
$sourceLon = '';
$radiusKm = 50;
$proximity = mathGeoProximity($sourceLat, $sourceLon, $radiusKm);
$result = mysql_query("
SELECT *
FROM locations
WHERE (lat BETWEEN " . number_format($proximity['latitudeMin'], 12, '.', '') . "
AND " . number_format($proximity['latitudeMax'], 12, '.', '') . ")
AND (lon BETWEEN " . number_format($proximity['longitudeMin'], 12, '.', '') . "
AND " . number_format($proximity['longitudeMax'], 12, '.', '') . ")
");
// fetch all record and check wether they are really within the radius
$recordsWithinRadius = array();
while ($record = mysql_fetch_assoc($result)) {
$distance = mathGeoDistance($sourceLat, $sourceLon, $record['lat'], $record['lon']);
if ($distance <= $radiusKm) {
$recordsWithinRadius[] = $record;
}
}
// and then print your results using a google map
// ...
// calculate geographical proximity
function mathGeoProximity( $latitude, $longitude, $radius, $miles = false )
{
$radius = $miles ? $radius : ($radius * 0.621371192);
$lng_min = $longitude - $radius / abs(cos(deg2rad($latitude)) * 69);
$lng_max = $longitude + $radius / abs(cos(deg2rad($latitude)) * 69);
$lat_min = $latitude - ($radius / 69);
$lat_max = $latitude + ($radius / 69);
return array(
'latitudeMin' => $lat_min,
'latitudeMax' => $lat_max,
'longitudeMin' => $lng_min,
'longitudeMax' => $lng_max
);
}
// calculate geographical distance between 2 points
function mathGeoDistance( $lat1, $lng1, $lat2, $lng2, $miles = false )
{
$pi80 = M_PI / 180;
$lat1 *= $pi80;
$lng1 *= $pi80;
$lat2 *= $pi80;
$lng2 *= $pi80;
$r = 6372.797; // mean radius of Earth in km
$dlat = $lat2 - $lat1;
$dlng = $lng2 - $lng1;
$a = sin($dlat / 2) * sin($dlat / 2) + cos($lat1) * cos($lat2) * sin($dlng / 2) * sin($dlng / 2);
$c = 2 * atan2(sqrt($a), sqrt(1 - $a));
$km = $r * $c;
return ($miles ? ($km * 0.621371192) : $km);
}
然后用你想要的結果做。 如果您需要它,您甚至可以通過AJAX調用實現此解決方案。
更新:如何將記錄輸出為json
// add this to the above php script
header('Content-type: application/json');
echo json_encode( $recordsWithinRadius );
exit();
更新:如何通過jquery中的AJAX調用加載json
// javascript/jquery code
$(document).ready(function()
{
$.getJSON('http://yourserver/yourscript.php', function(data)
{
$.each(data, function(key, record) {
// do something with record data
console.log(record);
});
});
});
對於每個點,您必須計算到中心點的距離(還有一個Google服務 )。 然后只繪制距離<= 50km的點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.