0

我想添加一个for循环来循环遍历array并将标记添加到地图中,我试图涉足一些东西,但我就是无法休息,纬度/经度正在被回显为(##.#########,-##.########)我尝试过的格式许多不同的方式,但似乎只是不想将其添加到地图中。

<script>
var initialLocation;
var map;
var markersArray = [];
var gMarkerLatitude = new Array();
var gMarkerLongitude = new Array();
var markers = [];
<?php $i = 0; while($stmt -> fetch()) { ?>  
gMarkerLatitude[<?php echo $i; ?>] = ["<?php echo $gLatitude; ?>"];
gMarkerLongitude[<?php echo $i; ?>] = ["<?php echo $gLongitude; ?>"];
<?php $i++; } ?>
function initialize() {
 var mapOptions = {
  zoom: 15,
  center: new google.maps.LatLng(56.7626362, -111.379652),
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  disableDefaultUI: false,
  zoomControl: true,
  zoomControlOptions: {
    style: google.maps.ZoomControlStyle.LARGE
  }
};

  map = new google.maps.Map(document.getElementById('gmaps'),
  mapOptions);

  google.maps.event.addListener(map, 'click', function(event) {
    var latitude = event.latLng.lat();
    var longitude = event.latLng.lng();
    deleteOverlays();
    addMarker(event.latLng);
    updateTextFields(latitude, longitude);
  });


  google.maps.event.addListener(marker, 'click', function(event) {
    showInfo(window);
  });

}

function updateTextFields(lati, longi) {
  $('#inputLatitude').val(lati);
  $('#inputLongitude').val(longi);
}


function addMarker(location) {
  marker = new google.maps.Marker({
    position: location,
    map: map
  });
  markersArray.push(marker);
}

try {
for (i = 0; i < gMarkerLongitude.length; i++) {
  var marker = new google.maps.Marker({
    map: map,
    position: new google.maps.LatLng(gMarkerLatitude[i],gMarkerLongitude[i])
  })
  alert(new google.maps.LatLng(gMarkerLatitude[i], gMarkerLongitude[i]));
}

}
catch(err) {
  alert(err);
}

function deleteOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}

function loadScript() {
  var script = document.createElement('script');
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&' +
  'callback=initialize';
  document.body.appendChild(script);
}


$(document).ready(function() {
$(window).resize(function () {
    var h = $(window).height(),
        offsetTop = 190; // Calculate the top offset

    $('#gmaps').css('height', (h - offsetTop));
  }).resize();
  loadScript();
});


</script>
4

2 回答 2

0

google.maps.LatLng将两个数字作为参数,但您传递的是一个包含字符串的数组。尝试

gMarkerLatitude[<?php echo $i; ?>] = <?php echo $gLatitude; ?>;
gMarkerLongitude[<?php echo $i; ?>] = <?php echo $gLongitude; ?>;

假设$gLatitude$gLongitude是数字

于 2013-03-05T01:21:32.327 回答
0

哪个部分抛出错误?您可以visible: true在创建每个标记时尝试添加到 markerOptions。还要确保您的 lat/lng 值在指定范围内

Latitude is specified in degrees within the range [-90, 90].
Longitude is specified in degrees within the range [-180, 180].
于 2013-03-05T03:28:40.980 回答