1

我正在获取一个数组,我想将一些数据插入到 javascript 中的两个位置,但我不知道如何正确执行,有人可以帮我吗?

我在 PHP/MYSQL 中获取一个数组,如下所示:

<?php while($row = mysql_fetch_array( $result )) { ?>

<?php
//I WANT TO LOOP THE BELOW CODE IN JAVASCRIPT SOMETHING LIKE THIS?: 
echo 'var point'.$count.'= new google.maps.LatLng('.$LAT.', '.$LNG.');'; 
?>

<?php
// I WANT TO LOOP THIS ALSO BUT IT IS AT THE END OF THE JAVASCRIPT CODE AND THERE IS CODE IN THE MIDDLE THAT I DON'T WANT LOOPED
echo 'var marker'.$count.'= new google.maps.Marker({draggable: true,raiseOnDrag: false,icon: image,shadow: shadow,shape: shape,map: map,position: point'.$count.'});'; 
?>

<?php } ?>

JavaScript 看起来像这样:

    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript">
    function load(){

 //** START LOOP THIS IN PHP **//    
    var point1 = new google.maps.LatLng(44.8041322, -93.1668858); //LOOP THIS IN PHP
 //** END LOOP THIS IN PHP **//    

    var myMapOptions = {
    zoom: 5,
    center: point,
    mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    var map = new google.maps.Map(document.getElementById("map"),myMapOptions);
    var image = new google.maps.MarkerImage(
    'marker-images/image.png',
    new google.maps.Size(40,35),
    new google.maps.Point(0,0),
    new google.maps.Point(20,35)
    );
    var shadow = new google.maps.MarkerImage(
    'marker-images/shadow.png',
    new google.maps.Size(62,35),
    new google.maps.Point(0,0),
    new google.maps.Point(20,35)
    );
    var shape = {
    coord: [27,0,30,1,32,2,34,3,35,4,36,5,38,6,39,7,39,8,39,9,39,10,38,11,37,12,33,13,34,14,34,15,33,16,32,17,31,18,27,19,28,20,28,21,27,22,26,23,22,25,23,26,24,27,24,28,24,29,24,30,24,31,24,32,23,33,22,34,17,34,16,33,15,32,15,31,14,30,14,29,15,28,15,27,16,26,17,25,13,23,12,22,11,21,11,20,12,19,8,18,7,17,6,16,5,15,5,14,6,13,2,12,1,11,0,10,0,9,0,8,0,7,1,6,3,5,4,4,5,3,7,2,9,1,12,0,27,0],
    type: 'poly'
    };

 //** START LOOP THIS IN PHP **//
    var marker1 = new google.maps.Marker({draggable: true,raiseOnDrag: false,icon: image,shadow: shadow,shape: shape,map: map,position: point1});
 //** END LOOP THIS IN PHP **//

    }
    </script>

我只是不知道如何循环信息两次,顶部的 var point1... 循环并不是真正的问题,但是我如何在不循环所有 javascript 的情况下循环底部

4

1 回答 1

1

下面应该可以工作,然后只需在 while 循环后的适当位置插入 $point_str 和 $marker_str 即可。

<?php 
$point_str = '';
$marker_str = '';
while($row = mysql_fetch_array( $result )) {
    // define $LAT, $LNG, $count++
    $point_str .= 'var point'.$count.'= new google.maps.LatLng('.$LAT.', '.$LNG.');'."\n";
    $marker_str .= 'var marker'.$count.'= new google.maps.Marker({draggable: true,raiseOnDrag: false,icon: image,shadow: shadow,shape: shape,map: map,position: point'.$count.'});'."\n";
}
?>
于 2012-12-11T16:46:16.670 回答