来自json的Google Maps V3 InfoWindow无法显示

时间:2014-01-22 20:25:52

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

我使用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>

知道我哪里错了吗?

1 个答案:

答案 0 :(得分:1)

您在函数InfoWnd中输入错误infoWnd而不是setInfoWindow()