在我的页面上,当它加载时它会获取您的位置并在您的位置设置一个标记,然后我希望它从我的数据库中加载所有标记并将它们设置在地图上,但我只想加载距离它们 1 公里以内的标记地点。我正在尝试设置它,但我遇到了一些麻烦。
所以标记是从数据库中加载的,如下所示:
<?php while($stmt -> fetch()) { ?>
var longi = "<?php echo $gLongitude; ?>";
var lati = "<?php echo $gLatitude; ?>";
var title = "<?php echo $gTitle; ?>";
setMarker(lati, longi, title);
<?php } $stmt -> close(); $mysqli -> close();?>
然后 setMarker 函数调用如下:
function setMarker(lati, longi, title) {
var latLongMarker = new google.maps.LatLng(lati,longi);
marker = new google.maps.Marker({
position: latLongMarker,
map: map,
draggable: false,
title: title
});
arrMarkers.push(marker);
}
这一切都很好,花花公子,并将数据库中的所有标记加载到地图上,但我怎么能只加载距离我 1 公里的标记呢?我阅读了有关 computeDistanceBetween() 的信息,但找不到可以挽救生命的示例。提前谢谢大家。
我最终找到了一种方法来完成它,它最终变得更容易和更快地处理在这里你去寻找任何人在未来寻找这个:
<?php while($stmt -> fetch()) { ?>
var longi = "<?php echo $gLongitude; ?>";
var lati = "<?php echo $gLatitude; ?>";
var title = "<?php echo $gTitle; ?>";
var content = 'Bus arrives at: ' + "<?php echo $gWeekdayDay; ?>";
database.push({latitude: lati, longitude: longi, markerTitle: title, content: content});
<?php } $stmt -> close(); $mysqli -> close();?>
for (var i = 0; i < database.length; i++) {
createMarker(database[i].latitude, database[i].longitude, database[i].markerTitle, initialLocation,
database[i].content);
}
function createMarker(lati, longi, title, myPosition, content) {
var latLongMarker = new google.maps.LatLng(lati, longi);
distanceCompare.push({position: latLongMarker, markerTitle: title});
setMarker(myPosition, latLongMarker, title, content);
}
function setMarker(myPosition, latLongMarker, title, content) {
distance = google.maps.geometry.spherical.computeDistanceBetween(latLongMarker, myPosition);
console.log('Distance of '+latLongMarker+ 'and original position' + myPosition + 'Is equal to '+distance);
updateResults();
if (distance < setDistance) {
addMarker(latLongMarker, title, content);
stopsfound++;
updateResults();
console.log(content);
}
}
function addMarker(position, title, content) {
console.log('Adding Marker ' + content);
marker = new google.maps.Marker({
position: position,
map: map,
title: title
});
bindInfoWindow(marker, content);
markersArray.push(marker);
}