2013-03-05 110 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].