【问题标题】:including a Google dynamic map包括谷歌动态地图
【发布时间】:2015-02-12 23:09:52
【问题描述】:

当我试图用 PHP 回调一个 JavaScript 函数时,我需要一些帮助。我创建了 2 个完美地协同工作的类。在第一个利用 Google API 的案例中,我提取了用户在我的表单上选择的城市的纬度和经度,然后我将这些值传递给第二个类,该类利用另一个 API 显示该城市的天气预报服务。

我也想添加一个谷歌地图,以这种方式创建一个更完整的服务,显示所选城市的动态地图。

我注意到 Firebug 浏览器无法正确解析对象 $map 的两个提到的属性,您可以自己轻松地看到。 我真的不知道怎么回调标签里面的js函数,我需要。

我在这里与 PHP 一起使用的简单回显在这里不起作用。

希望有人可以帮助我。 这里是my page

这是我的一段代码不起作用:

        /**
         * Check for the real existence of the city, using the 
         * property $status of the instanced GeoLocalization class, here the object $map.
         * This class exploits a Google API.
         */
         if ( isset($missing) && empty($missing) && strlen($citta) > 1 && $map->status == 'OK' )
            {    
                /**
                * Use some properties of the object $map to show my user 
                * the chosen city and state in Italian.
                * Show even the latitude and the longitudine.
                * These values are those passed to the GeoWeather class.
                */  

                // show the location                
                echo '<ul id="display_location">' . 
                        '<li class="rosso centra sottolineato grassetto">' . $map->formatted_address . '</li>' .
                        '<li>' . 'Latitudine: ' . $map->latitude . '</li>' .
                        '<li>' . 'Longitudine: '  . $map->longitude . '</li>' .
                      </ul>';

               echo '<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>';                

               echo '<script type="text/javascript">
                        var map;
                        function initialize() {
                            var mapOptions = {
                            zoom: 8,
                            center: new google.maps.LatLng(<?php echo $map->latitude . ", "  . $map->longitude; ?>)
                            };
                        map = new google.maps.Map(document.getElementById("googleStaticMap"), mapOptions);
                        }
                    </script>';

                echo '<script type="text/javascript">initialize();</script>';

                echo '<div id="googleStaticMap"></div>'; 
            }
  </body>

【问题讨论】:

    标签: javascript php google-maps google-maps-api-3


    【解决方案1】:

    您不能在 PHP 块内启动新的 PHP 块。

    这个:

           echo '<script type="text/javascript">
                    var map;
                    function initialize() {
                        var mapOptions = {
                        zoom: 8,
                        center: new google.maps.LatLng(<?php echo $map->latitude . ", "  . $map->longitude; ?>)
                        };
                    map = new google.maps.Map(document.getElementById("googleStaticMap"), mapOptions);
                    }
                </script>';
    

    应该是:

               echo '<script type="text/javascript">
                        var map;
                        function initialize() {
                            var mapOptions = {
                            zoom: 8,
                            center: new google.maps.LatLng('. $map->latitude . ','  
                                                            . $map->longitude .')
                            };
                        map = new google.maps.Map(document.getElementById("googleStaticMap"), 
                                                  mapOptions);
                        }
                    </script>';
    

    其他问题:

    你必须切换这2行的顺序(否则调用函数时#googleStaticMap是未知的):

                echo '<script type="text/javascript">initialize();</script>';
    
                echo '<div id="googleStaticMap"></div>';
    

    另外:

    #googleStaticMap 目前没有高度。您已将高度设置为 100%,但只要 #googleStaticMap 的父元素没有通过 CSS 设置高度,则不会产生任何影响。

    【讨论】:

    • 我尝试按照您现在看到的所有建议进行更改,但我仍然看不到这张该死的动态地图。我现在一点头绪都没有!一切都应该没问题,但是.... 想法?谢谢。
    • 地图仍然没有高度
    • 我为地图添加了一个容器 ID,并为它指定了宽度和高度,然后我在地图 ID 本身中重复了这一点。我在 部分实现了这个 CSS 样式,现在效果很好。你可以放任何你喜欢的城市,你会看到动态地图的结果。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多