【问题标题】:Google Maps V3 InfoWindow from json not displaying来自json的Google Maps V3 InfoWindow不显示
【发布时间】:2014-02-13 02:53:08
【问题描述】:

我正在使用 php 从 MySQL 数据创建一个 json 文件。 当我点击标记时,我想做的是在谷歌地图的信息窗口中显示其中的一些数据。

目前我可以让所有标记出现,但我收到错误:

Uncaught ReferenceError: InfoWnd is not defined

我认为我在设置信息窗口的方式上犯了一些错误,但我尝试了几种方法,但总是收到相同的错误消息。

到目前为止,这是我的 javascript:

** 初始化地图画布:

<script type="text/javascript">
            var map;
    function initialize() {
                    var mapOptions = {
                      center: new google.maps.LatLng(48.476, -81.312),
                      zoom: 18,
                      mapTypeId: google.maps.MapTypeId.SATELLITE
                    };
                    map = new google.maps.Map(document.getElementById("map_canvas"),
                            mapOptions);
    }
    </script>
<body onload="initialize()">
    <div id="map_canvas" style="width=80%; height: 100%"></div>

** 并创建标记/信息窗口:

<script type="text/javascript">
    var blueIcon = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
    var redIcon = "http://www.google.com/intl/en_us/mapfiles/ms/micons/red-dot.png";
    var greenIcon = "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png";
    infoWnd = new google.maps.InfoWindow();

    $(document).ready(function() {
        $.ajax({
            url: 'http://localhost/testing/map_json.php',
            dataType: 'json',
            success: function (jsonData) {
                $.each(jsonData, function(key, data) {
                     var latLng = new google.maps.LatLng(data.lat, data.lng);
                     // creating marker, putting it on map

                     // If no sensor reading data is present, RTU is assumed to be repeater node.
                     // Displayed in blue and Info Window only displays system voltage and time of reading.
                     if ( data.R_1 === undefined ) {
                          if ( data.SysV >= 6.5 ) {
                               var marker = new google.maps.Marker({
                                   position: latLng,
                                   title: data.RTU_Addr,
                                   icon: blueIcon
                                });
                                setInfoWindow();
                           }
                           else {
                               var marker = new google.maps.Marker({
                                   position: latLng,
                                   title: data.RTU_Addr,
                                   icon: redIcon
                                });
                                setInfoWindow();
                           }
                      }
                      // If sensor data is present, RTU is assumed to be end unit.
                      // Displayed in red/green and Info Window displays all readings along with timestamp and voltage.
                      else {
                          if ( data.SysV >= 6.5 ) {
                              var marker = new google.maps.Marker({
                                  position: latLng,
                                  title: data.RTU_Addr,
                                  icon: greenIcon
                               });
                               setInfoWindow();
                           }
                           else {
                               var marker = new google.maps.Marker({
                                   position: latLng,
                                   title: data.RTU_Addr,
                                   icon: redIcon
                                });
                                setInfoWindow();
                            }

                        }
                        marker.setMap(map);

                        function setInfoWindow() {
                            google.maps.event.addListener(marker, 'click', function() {
                                infoWnd.setContent("hi");
                                InfoWnd.open(map, marker);
                             });
                         };
                     });    
                 }
             });
        });
    </script>

知道我哪里出错了吗?

【问题讨论】:

    标签: javascript ajax json google-maps google-maps-markers


    【解决方案1】:

    您在函数 setInfoWindow() 中有一个拼写错误 InfoWnd 而不是 infoWnd

    【讨论】:

    • 哇。我猜你盯着某样东西看够久了,你就会错过那些东西。谢谢!
    猜你喜欢
    • 2011-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-09
    • 2011-05-16
    • 2011-06-21
    • 2011-01-03
    • 2010-12-06
    相关资源
    最近更新 更多