我正在尝试使用一些函数来计算并仅显示距离其位置一定距离内的标记的距离,但该函数在页面加载后立即被强制转换,因此它使用了不正确的值
这是我从数据库生成标记的函数:
<?php while($stmt -> fetch()) { ?>
var longi = "<?php echo $gLongitude; ?>";
var lati = "<?php echo $gLatitude; ?>";
var title = "<?php echo $gTitle; ?>";
createMarker(lati, longi, title);
<?php } $stmt -> close(); $mysqli -> close();?>
然后调用 createMarker
function createMarker(lati, longi, title) {
var latLongMarker = new google.maps.LatLng(lati, longi);
marker = new google.maps.Marker({
position: latLongMarker,
map: map,
title: title
});
arrMarker.push(marker);
marker.setMap(null);
setMarker(latLongMarker);
}
然后调用 setmarker
function setMarker(position) {
console.log(arrMarker.value);
for (var i = 0; i < arrMarker.length; i++) {
var latLong = arrMarker[i].getPosition();
distance = google.maps.geometry.spherical.computeDistanceBetween(latLong, position);
if (distance < 4000) {
addMarker(arrMarker[i].getPosition());
}
}
}
问题是在使用以下方式设置地理位置之前它一直被调用:
function locateMe() {
if (navigator.geolocation) {
browserSupportFlag = true;
navigator.geolocation.getCurrentPosition(function(position) {
initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
map.setCenter(initialLocation);
map.setZoom(15);
setMarkerPosition(position.coords.latitude,position.coords.longitude);
}, function() {
handleNoGeolocation(browserSupportFlag);
});
}
}