0

我很难用动态内容填充 Google InfoWindows。我的计划是加载许多标记(其中 60 个)并为每个标记附加一个单击事件处理程序。单击每个标记时,我想根据唯一 ID 列从融合表中获取相应的数据。简而言之,每个标记都有与之关联的不同数据,没有两个标记会将相同的数据集加载到各自的 infowWindow 中。我读到了关于闭包的文章,但似乎无法将这个概念正确地应用于这个问题集——在这里寻找建议。

            var markers = new Array();
            infoWindow = new google.maps.InfoWindow();


            function getBeachLocations() {
                // construct query
                var queryURL = "https://www.googleapis.com/fusiontables/v1/query?sql=";
                var queryTail = "&key=apiKey=?";
                var query = "SELECT * FROM beachLocationTable"; // beach locations tbl
                var queryText = encodeURI(query);

                $.ajax({
                    type: "GET",
                    url: queryURL + queryText + queryTail,
                    cache: false,
                    dataType: 'jsonp',
                    success: createMarkers,
                    error: function () {
                        alert("Sorry, no spatial data is available at this time, please check back on a later date.");
                    }

                });
            } //end getBeachLocations


            function createMarkers(data) {



                url = "swim.jpg";
                var rows = data['rows'];


                for (var m in rows) {

                    var ecoli_array = new Array();
                    var marker = new google.maps.Marker({
                        map: map,
                        icon: new google.maps.MarkerImage(url),
                        beach_id: rows[m][0],
                        beach_name: rows[m][1],
                        beach_region: rows[m][2],
                        position: new google.maps.LatLng(rows[m][4], rows[m][5]),
                        idx: m,
                        getEcoliData: function () {

                            var obj;

                            var ecoli_rows_str = "";

                            obj = getEcoliData(marker.beach_id);

                            obj.success(function (data) {
                                var rows;
                                rows = data['rows'];
                            });
                            for (var i = 0; i < rows.length; i++) {
                                console.log(rows[i]);
                                ecoli_rows_str += '<tr><td>' + rows[i][2] + '</td><td>' + rows[i][3] + '</td><td>' + rows[i][4] + '</td></tr>';
                            }
                            return ecoli_rows_str;
                        } // end getEcoliData
                    });               // end marker

                    google.maps.event.addListener(marker, 'click', (function (marker) {
                        return function () {
                            infoWindow.setContent("ID is " + this.beach_id + "</br>Beach Name is: " + this.getEcoliData());
                            infoWindow.open(map, this);
                        }
                    })(marker));

                } // end for loop

            } //end function createMarkers


            function getEcoliData(beach_id) {
                console.log(beach_id);
                var rows;

                var queryURL = "https://www.googleapis.com/fusiontables/v1/query?sql=";
                var queryTail = '&key=apiKey=?';
                var whereClause = "WHERE 'Beach_ID' = " + beach_id;
                var query = "SELECT * FROM ecoliTable "
                + whereClause + " ORDER BY 'Sample_Date' DESC";


                var queryText = encodeURI(query);

                var ecoli_array = new Array();

                return $.ajax({
                    type: "GET",
                    url: queryURL + queryText + queryTail,
                    cache: false,
                    dataType: 'jsonp'

                });
            }


            // create beach locations dropdown
            function createDropDownMenu() {
                var query = 'SELECT Beach_Location, COUNT()  FROM   beachLocationTable GROUP BY Beach_Location ORDER BY Beach_Location ASC';

                var queryText = encodeURIComponent(query);
                var gvizQuery = new google.visualization.Query(
                                                'http://www.google.com/fusiontables/gvizdata?tq=' + queryText);

                //Send query and draw table with data in response
                gvizQuery.send(function (response) {
                    var numRows = response.getDataTable().getNumberOfRows();
                    var numCols = response.getDataTable().getNumberOfColumns();

                    var name = ['<label style="font-weight:bolder;font-size:16px"> Select a Beach:</label><select id="beach_menu"  style="font-size:16px;" onchange="select_beach(this.options[this.selectedIndex].value);"><option class="defaultopt" value="">--All--</option>'];

                    for (var i = 0; i < numRows; i++) {
                        var nameValue = response.getDataTable().getValue(i, 0);

                        name.push("<option value=" + "'" + nameValue + "'" + ">" + nameValue + "</option>");

                    }
                    name.push('</select>');

                    document.getElementById('beach_location_dropdown').innerHTML = name.join('');
                });

            } // end createDropDownMenu Function

            /* start map initialization  */
            function initialize() {


                // map
                latlng = new google.maps.LatLng(49.894634, -97.119141);
                var myOptions = {
                    center: latlng,
                    zoom: 7,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                    zoomControlOptions: {
                        style: google.maps.ZoomControlStyle.SMALL
                    },
                    mapTypeControl: true,
                    mapTypeControlOptions: {
                        mapTypeIds: [
                                                        google.maps.MapTypeId.ROADMAP,
                                                        google.maps.MapTypeId.SATELLITE,
                                                        google.maps.MapTypeId.HYBRID,
                                                        google.maps.MapTypeId.TERRAIN
                                                    ],

                        style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
                    },
                    overviewMapControl: true,
                    overviewMapControlOptions: {
                        opened: true
                    }
                };

                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);


                // invocation begins here
                createDropDownMenu(); // not working now.
                getBeachLocations();




                // legend
                map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(
                    document.getElementById('legend'));

                var legend = document.getElementById('legend');
                var swim_icon = "swim.jpg";


                var div = document.createElement('div');
                div.innerHTML = '<h4>Features</h4>' +
                                                '<br/><img src="' + swim_icon + '"> ' + "Beaches";
                legend.appendChild(div);

            } // end initialization function
4

2 回答 2

0

通过使用 FusionTables 界面加入您的表格,您可能会得到最好的服务。

在 FusionTables 中使用菜单“文件”,然后选择“查找要合并的表”。

最后,您的最终结果应该是包含所有 3 个数据的单个表,您可以通过单个查询将其定位在信息窗口中。

我希望这会有所帮助。

于 2013-10-22T15:04:12.613 回答
0

感谢所有为这篇文章做出贡献的人。事实证明这很简单,我正在查询一个错误的表,该表抛出了结果。我设法将正确的内容放入 infoWindow。现在我只需要决定当数组为空、null 或未定义时如何最好地在 infoWindow 中向用户显示消息。以下就足够了吗?[代码]

if (typeof rows == 'undefined') { rows_str = '

抱歉,目前没有此海滩的生态数据。

' } else { //将列行数据添加到字符串 }

[/代码]

与往常一样,非常感谢这里的任何建议。干杯,迈克尔

于 2013-10-22T17:01:34.787 回答