2011-12-28 93 views
0

我在我的數據庫中有經度和緯度現在我想在Google地圖v3上顯示多個標記,但問題是當我檢索我的價值(拉特,長)只有一個標記顯示在地圖上因爲環和其他價值的獲得最後的值被覆蓋..在谷歌地圖v3顯示經緯度Api

我的代碼是這個..

<?php foreach($this->searchResult as $result): ?> 
<ul> 
<li><?php echo $result['user_id']; ?> 

<li><?php echo $result['name']; ?></li> 
<li><?php echo $result['Latitude']; ?></li> 
<li><?php echo $result['Longitude']; ?></li> 


<li><?php if($result['country'] == '1'){echo "America"; }else{ echo "";} ?></li> 
<a href="<?php echo 
$this->url(array('controller'=>'search', 'action'=>'get-Details',   'id'=>$result['id']))?>">Details</a> 
</ul> 
<script type="text/javascript"> 
var berlin = new google.maps.LatLng(40.7144, -74.0061); 

    var lat = new Array(); 
    var lon = new Array(); 
    var lat = <?php echo $result['Latitude']; ?>; 
    var lon = <?php echo $result['Longitude']; ?>; 


var neighborhoods = [ 

    new google.maps.LatLng(lat,lon) 

]; 


var markers = []; 
var iterator = 0; 

var map; 

function initialize() { 
var mapOptions = { 
    zoom: 4, 
    mapTypeId: google.maps.MapTypeId.ROADMAP, 
    center: berlin 
}; 


    map = new google.maps.Map(document.getElementById("map_canvass"), 
     mapOptions); 

}

function drop() { 
for (var i = 0; i < neighborhoods.length; i++) { 
    setTimeout(function() { 
    addMarker(); 
    }, i * 200); 
} 

}

function addMarker() { 
    markers.push(new google.maps.Marker({ 
    position: neighborhoods[iterator], 
    map: map, 
    draggable: false, 
    animation: google.maps.Animation.DROP 
})); 
    iterator++; 
} 
</script> 


<?php endforeach;?> 



<?php echo $this->paginationControl($this->searchResult, 'Sliding', 'pagination.phtml'); ?> 

<div id="map_canvass" style="width:800px; height:300px"></div> 

<button id="drop" onclick="drop()">Drop Markers</button> 

回答

0

不要在foreach循環中創建腳本標記。更好的方法是生成一個數據對象並將其作爲JS變量呈現到腳本塊中。然後你可以在JS中通過這個數據對象循環並添加標記。

json_encode是打印數據對象的好方法。 http://www.php.net/manual/en/function.json-encode.php

僞代碼:

<script type="text/javascript"> 
    (function() { 
     var neighborhoods = <?php echo json_encode($allHoodsIWantToAdd);?>; 
     ... 
    }()); 
</scritp> 
+0

其實我只是想存儲所有的緯度,經度價值在這裏..然後把它拿來.. 但其只保留最後的價值.. VAR社區= [ new google.maps.LatLng(lat,lon) ]; – 2011-12-29 14:30:52

+0

等於運算符進行分配並覆蓋以前的分配。要擴展一個javascipt數組,你可以使用push()函數。 'var neighborhoods = []; neighborhoods.push(firstCoordinate); neighborhoods.push(secondCoordinate);' – 2011-12-29 17:09:34

+0

或者同時分配所有元素。 'var neighborhoods = [firstCoordinate,secondCoordinate];' – 2011-12-29 17:12:44