Drupal - 将PHP变量传递给JavaScript的问题?

时间:2014-05-05 15:52:22

标签: javascript php google-maps drupal drupal-7

我构建了一个自定义谷歌地图php文件,以实现到我的drupal网站的首页,该网站同时使用javascript和php,并通过template.php文件中的drupal_add_js进行调用。我很难弄清楚如何在javascript中正确运行PHP,然后将结果设置为JavaScript变量markers。我试图将每个结果推送到一个数组并通过json_encode($item);回显数组但是,在我的控制台中,我收到的错误是Cannot read property 'lat' of undefined

这是我的整个map.php:

var map = null;
var infowindow = new google.maps.InfoWindow();
var markers = [Drupal.settings.markers];

window.onload = function () {

var mapOptions = {
    center: new google.maps.LatLng(
        parseFloat(markers[0].lat),
        parseFloat(markers[0].lng)),
    zoom: 13,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
var path = new google.maps.MVCArray();
var service = new google.maps.DirectionsService();
var infoWindow = new google.maps.InfoWindow();
var map = new google.maps.Map(document.getElementById("map"), mapOptions);
var poly = new google.maps.Polyline({map: map, strokeColor: '#F3443C'});

var lat_lng = new Array();
           for (i = 0; i < markers.length; i++) {
           var data = markers[i]
           var myLatlng = new google.maps.LatLng(data.lat, data.lng);
           lat_lng.push(myLatlng);
           var marker = new google.maps.Marker({
               position: myLatlng,
               map: map,
               title: data.title
           });
           (function (marker, data) {
               google.maps.event.addListener(marker, "click", function (e) {
                   infoWindow.setContent(data.description);
                   infoWindow.open(map, marker);
               });
           })(marker, data);
       }
for (var i = 0; i < markers.length; i++) {
    if ((i + 1) < markers.length) {
        var src = new google.maps.LatLng(parseFloat(markers[i].lat), 
                                         parseFloat(markers[i].lng));
        var des = new google.maps.LatLng(parseFloat(markers[i+1].lat), 
                                         parseFloat(markers[i+1].lng));
        service.route({
            origin: src,
            destination: des,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        }, function (result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                for (var i = 0, len = result.routes[0].overview_path.length; i < len; i++) {
                    path.push(result.routes[0].overview_path[i]);
                }
                poly.setPath(path);
            }
        });
    }
}
} 

这是我的页面 - front.tpl.php,我试图创建可以访问我的js文件的数组:

<?php

  if (isset($_GET['routeselected'])) 
  {
  $result = mysqli_query($db, "SELECT * FROM STOP INNER JOIN RouteStop ON Stop.Stop_ID = RouteStop.Stop_ID WHERE RouteStop.Route_ID = " . (int)$_GET['routeselected'] . " AND RouteStop.Company_ID = " . (int)$_GET['companyselected']);
  $markers = array();
  while (($row = mysqli_fetch_array($result))){
      $markers[$row['title']] = array($row['Stop_ID']);
      $markers[$row['lat']] = array($row['Latitude']);
      $markers[$row['lng']] = array($row['Longitude']);
      $markers[$row['description']] = array($row['StopName']);
      }
    drupal_json_encode($markers);
    drupal_add_js(array('markers' => drupal_json_encode($markers)), 'setting');
  }
  ?>

有没有人可能知道我的问题的解决方案?谢谢大家和任何帮助!

1 个答案:

答案 0 :(得分:2)

在javascript文件中编写内联PHP代码确实是一个坏主意。

解决方案是:

  • 将该PHP代码移至模块
  • 生成$ markers作为PHP关联数组/对象
  • 使用drupal_json_encode($markers)
  • 将其转换为JSON
  • 使用将其添加到Drupal.settings.markers drupal_add_js(array('markers' => drupal_json_encode($markers)), 'setting');
  • 在你的js中使用var markers = Drupal.settings.markers

有关详细信息,请查看此文章:https://drupal.org/node/756722#settings-javascript