duanpu8830 2015-12-26 23:36
浏览 60

gmaps.js从PHP数组中添加标记

I am trying to add a marker to my gmaps.js map. The long. and lat. values are stored in a database, which I select using a PHP Script.

Once fetched, both of the long. and lat. values are stored in an array, under the variable $data.

I wanted to use json_encode($data); in order to pass the variables, however I would have had to change the header to a json application, I have already echoed some PHP onto the page, therefore this is unfeasable.

I have now tried echoing the json_encode into the jQuery function using an $.each loop, however it has been unsuccessful, as not every entry retrieved by the database will have a marker.

What is the best solution, to get an ID, long. and lat. value for max 10 records, pass this to a jQuery function to position on a map?

  • 写回答

1条回答 默认 最新

  • dter8514 2015-12-27 00:36
    关注

    As not all records have markers, youll have to parse the results so that only the required data gets passed to your js function. I'd create a php array and add the marker I'd, lat,long etc... Under the same key:

    $markersarray = array ()
    foreach($records as $record){
    
      if($record['lat'] != ““){
        $markersarray[] = array(
             'id'=>$record['id'], 
             'lat'=>$record['lat'],
             'long'=>$record['long']
        );
      }
    }
    
    $markersjson = json_encode($markersarray);
    

    Then you could use a hidden input to store the encoded json string which then could be passed into your js.

     <input id="markersjson" type="hidden" value="<?php echo $markersjson; ?>"/>
    

    in your js function

    var markersdata = $('#markersjson').val();
    

    You may need to parse this value to an object

    var obj = jQuery.parseJSON(markersdata);
    

    then use a $.each to loop through your obj

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭