0

我正在尝试在我的 php 网站上获取网站 API 的谷歌地图。为此,我有一个为地图加载用户变量的 php 页面。javascript 在具有默认值的网页中运行良好,但我无法将 $php 变量加载到脚本中。

也许一些 javascript 专家可以帮忙

function onProfileDisplay() {
            $user           = CFactory::getRequestUser();
            $document       =& JFactory::getDocument();
            $document->addStyleSheet($css);
            $my         = CFactory::getUser();
            $pluginParams = $this->params;
            $param = new stdClass;
            $param = $Lat = $pluginParams->get('Lat');
            $param = $Lng = $pluginParams->get('Lng');
            $param = $width = $pluginParams->get('width');
            $param = $height = $pluginParams->get('height');
            $param = $zoom = $pluginParams->get('zoom');
            return '<body onload="onprofileDisplay()">  <div id="map_canvas" style="width:800px; height:500px"></div>
                    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
                    <script type="text/javascript">

 function onProfileDisplay() {
            var latlng = new google.maps.LatLng(57.0442, 9.9116);
            **var zoom = <?php echo ($zoom) ?>;**
            var settings = {
                    **zoom: zoom,**
                    center: latlng,
                    mapTypeControl: true,
                    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                    navigationControl: true,
                    navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
            var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
            var companyPos = new google.maps.LatLng(57.0442, 9.9116);
            var companyMarker = new google.maps.Marker({
            position: companyPos,
            map: map,
            title:"Some title"
            });
            }
            </script>
                    ';
            }

测试我只是试图将 php 变量 $zoom 加载到 javascript 中,但它在 javascript 内部不起作用:

var zoom = <?php echo ($zoom) ?>;
zoom: zoom,

但它不起作用,如果我将 javascript 更改为原始 remove var zoom = ; 缩放:15,然后它可以工作,但使用静态默认值

帮助表示赞赏。

这是输出代码:

<script type="text/javascript">
    function onProfileDisplay() { 
    var latlng = new google.maps.LatLng(57.0442, 9.9116);
    var zoom = ".$zoom.";
            var settings = {
                    ".$zoom.":".$zoom.",
                    center: latlng,
                    mapTypeControl: true,
                    mapTypeControlOptions: {style:   google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                    navigationControl: true,
                    navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                    };
            var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
            var companyPos = new google.maps.LatLng(57.0442, 9.9116);
            var companyMarker = new google.maps.Marker({
            position: companyPos,
            map: map,
            });
            }
    </script>

原始代码如下所示:

     public function onProfileDisplay() {
                $user           = CFactory::getRequestUser();
                $document       =& JFactory::getDocument();
                $document->addStyleSheet($css);
                $my         = CFactory::getUser();
                $pluginParams = $this->params;
                $param = new stdClass;
                $param = $api_key = $pluginParams->get('api_key');
                $param = $Lat = $pluginParams->get('Lat');
                $param = $Lng = $pluginParams->get('Lng');
                $param = $width = $pluginParams->get('width');
                $param = $height = $pluginParams->get('height');
                $param = $zoom = $pluginParams->get('zoom');
                 return  '<body onload="onProfileDisplay()">
                        <div id="map_canvas" style="width:800px; height:500px"></div
</body>
                        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
                        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
                        <script type="text/javascript">
                function onProfileDisplay() {
                var latlng = new google.maps.LatLng(57.0442, 9.9116);
                var zoom = ".$zoom.";
                var settings = {
                        ".$zoom.":".$zoom.",
                        center: latlng,
                        mapTypeControl: true,
                        mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
                        navigationControl: true,
                        navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                        };
                var map = new google.maps.Map(document.getElementById("map_canvas"), settings);
                var companyPos = new google.maps.LatLng(57.0442, 9.9116);
                var companyMarker = new google.maps.Marker({
                position: companyPos,
                map: map,
                });
                }
                </script>';
                }
    }
    ?>
4

1 回答 1

0

为什么将 PHP 放入 PHP->javascript 函数的返回中?似乎获得结果的方法很繁琐。

无论如何 - 为什么不把 PHP 函数的实际值放在那里呢?一旦它们在客户端的 javascript 中,您将无法使用服务器端值。

例子:

函数 onProfileDisplay() {
var latlng = new google.maps.LatLng(57.0442, 9.9116);
var zoom = " . $zoom ."
var settings = {
" . $zoom . ":" . $zoom . ",
center: latlng,

于 2012-05-08T14:45:26.300 回答