2013-03-05 63 views
0

我想添加一个for循环来循环通过array并将标记添加到地图,我试图涉足一些东西,但我只是不能休息一下,纬度/经度被回显为(##.#########,-##.########)格式我尝试了许多不同的方式,但它似乎不想将其添加到地图。使用for循环与谷歌地图添加一个标记

<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> 

回答

0

google.maps.LatLng需要两个数字作为参数,但你传递一个数组,在它的字符串。尝试

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

假设$gLatitude$gLongitude是数字

+0

他们确实是十进制值。我会在我离开工作之前得到一个错误,当我真正快速到家时,您是否可以使用原始文章中的脚本编辑您的文章并替换您的值我想我可能已经理解导致错误 – Datsik 2013-03-05 02:32:49

0

哪一部分引发错误?创建每个标记时,您可以尝试将visible: true添加到markerOptions。还要确保你的纬度/经度值在specified ranges

Latitude is specified in degrees within the range [-90, 90]. 
Longitude is specified in degrees within the range [-180, 180].