gpt4 book ai didi

javascript - 显示给定半径内数据库中的所有位置

转载 作者:行者123 更新时间:2023-11-29 10:20:59 25 4
gpt4 key购买 nike

我想使用谷歌地图检索并显示给定点的固定半径范围内的所有位置。

我设法找到了 Display Location guide并且还看到了许多关于使用 SQL 查询检索它的帖子。我的数据库包含项目名称、地址(查找 alt、lon)、alt、lon 和描述。我如何使用存储的 alt, lon 仅检索位于(假设半径为 50 公里)范围内的内容。

这是我的代码:

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 guide DisplayLocations() 方法,但它对我不起作用,虽然位置呈现的方式很完美,只需要排除半径范围之外的位置。

最佳答案

可能不完全是您所期望的,但我希望它能有所帮助。从您发布的链接来看,我认为您使用的是 PHP/MySQL。

如果这是真的,我会使用 PHP/MySQL 获取正确的结果,然后将它们显示在 Google map 上。

如果您无需外部服务即可进行计算,效率会更高。

// 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);
});
});
});

关于javascript - 显示给定半径内数据库中的所有位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12128625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com