用json编码的数组循环谷歌地图标记

问题描述:

Really need help here. I convert an array with json_encode to be able to loop through the lats/longs of the searchresult in a google map below. The map loads fine if i just use a normal marker, and if I echo the json encoded array I get:

["55.7171, 13.2354","55.6411, 13.2122"]

The Php code below (ignore most of it except the markers part, just giving you an idea of how it´s used).

<?php
    if (isset($search_result)) {

        $markers = array();

        // Looping through the results and displaying appropriate data.
        foreach ($search_result as $result) {
            ?>
            <li><?= $result['first_name']; ?></li>
            <li><?= $result['last_name']; ?></li>
            <li><?= $result['address']; ?></li>
            <li><img width="80" height="80" src="<?php echo file_exists($result['profile_picture'])? $result['profile_picture'] : $img_profile_def; ?>"></li>
            <li><a href="profile.php?userid=<?= $result['id']; ?>">View This User</a></li><br><br>
    <?php
        $markers[] = $result['latitude'] . ', ' . $result['longitude'];
        }
    }
?>

The Javascript at the bottom of the script initializing the map etc. I get a map with this code but no markers.

<script type="text/javascript">
function initialize() {
    var markers = <?php echo json_encode($markers); ?>;

    var myLatLng = new google.maps.LatLng( <?php echo $area_coords; ?> );

    var mapOptions = {
        center:myLatLng,
        zoom:15,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);

    for (i = 0; i < markers.length; i++) {

    var position = new google.maps.LatLng(markers[i]);

    var marker = new google.maps.Marker({

        map:map,
        position:position

    });
    }
}

initialize();

这里真的需要帮助。 我用json_encode转换一个数组,以便能够在下面的谷歌地图中循环搜索结果的lats / longs。 如果我只使用普通标记,地图加载正常,如果我回显json编码数组,我得到: p>

  [“55.7171,13.2354”,“55.6411,13.2122”]  
  code>  pre> 
 
 

下面的Php代码(忽略大部分代码除了标记部分,只是让你知道它是如何使用的)。 p> &lt;?php if(isset($ search_result)){ $ markers = array(); //循环结果并显示相应的数据。\ n foreach($ search_result as $ result){ ?&gt; &lt; li&gt;&lt;?= $ result ['first_name']; ?&gt;&lt; / li&gt; &lt; li&gt;&lt;?= $ result ['last_name']; ?&gt;&lt; / li&gt; &lt; li&gt;&lt;?= $ result ['address']; ?&gt;&lt; / li&gt; &lt; li&gt;&lt; img width =“80”height =“80”src =“&lt;?php echo file_exists($ result ['profile_picture'])?$ result [' profile_picture']:$ img_profile_def;?&gt;“&gt;&lt; / li&gt; &lt; li&gt;&lt; a href =”profile.php?userid =&lt;?= $ result ['id'];?&gt ;“&gt;查看此用户&lt; / a&gt;&lt; / li&gt;&lt; br&gt;&lt; br&gt; &lt;?php $ markers [] = $ result ['latitude']。 ','。 $ result ['longitude']; } } ?&gt; code> pre>

脚本底部的Javascript初始化地图等我 获取具有此代码但没有标记的地图。 p>

 &lt; script type =“text / javascript”&gt; 
function initialize(){
 var markers =&lt;?  php echo json_encode($ markers);  ?&gt ;; 
 
 var myLatLng = new google.maps.LatLng(&lt;?php echo $ area_coords;?&gt;); 
 
 var mapOptions = {
 center:myLatLng,
 zoom:15  ,
 mapTypeId:google.maps.MapTypeId.ROADMAP 
}; 
 var map = new google.maps.Map(document.getElementById(“map_canvas”),
 mapOptions); 
 
 for(i =  0; i&lt; markers.length; i ++){
 
 var position = new google.maps.LatLng(markers [i]); 
 
 var marker = new google.maps.Marker({
 \  n map:map,
 position:position 
 
}); 
} 
} 
 
initialize(); 
  code>  pre> 
 
 

p > div>

I think it's because you're passing a string as the LatLng parameter instead of two floats...perhaps you could change your JSON so that it's: [[lat,lng],[lat,lng]...] like so:

$markers[] = [$result['latitude'],$result['longitude']];

and then

var position = new google.maps.LatLng(markers[i][0],markers[i][1]);