【问题标题】:In Fusion Tables and Google Maps API, how do I change the Tables layer and the Map position based on dropdown menu selection?在 Fusion Tables 和 Google Maps API 中,如何根据下拉菜单选择更改 Tables 图层和地图位置?
【发布时间】:2012-12-02 23:34:45
【问题描述】:

我正在尝试制作一个使用不同 Fusion Tables 的网站。有一个下拉菜单有几个状态。每个州都有自己的融合表

我想这样当你选择一个州时,地图会加载那个州的融合表。

我该怎么做?

以下是我目前的 JavaScript 代码(文件名是“script.js”)。下面是 HTML 代码。

当我通过下拉菜单更改状态时,我希望地图加载下拉菜单中状态的 Fusion Table 地图。现在,它不这样做。

$(document).ready(function(){
    var states = {
        'Alabama':'3348442',
        'Alaska':'3370522'
    };
    // SECTION 1: ADDING HTML TO DOM
    var bodyDivs = [
        '<div id="map-canvas" />',
        '<div id="address-form-container" />',
        '<div id="state-select-container"><select id="state-select" /></div>'
    ];

    var addressContainer = [
        '<span id="form pretext"> Enter a city name or address </span>',
        '<input id="address" type="textbox" />',
        '<input type="button" value="Search" />'
    ];

    // Append the body to <html>
    $('html').append('<body />');
    for (var i=0; i<bodyDivs.length; i++){
        $('body').append(bodyDivs[i]);
    };

    // Append the address form to <body>
    for (var i=0; i<addressContainer.length; i++){
        $('div#address-form-container').append(addressContainer[i]);
    };

    // Append state names to the body
    for (var state in states){
        $('select').append('<option value='+state+'>'+state+'</option>');
    };
    // DONE ADDING HTML TO DOM


    // SECTION 2: Create the Google map with the coordinates and add the Fusion Tables layer to it. Plus, it zooms and centers to whatever state is selected.
    var geocoder;
    var gmap;
    var locationColumn = "'geometry'";
    var condition = "'Median income (dollars)'>0";
    var selectedState = document.getElementById("state-select").value;
    var tableId = states[selectedState];    
    var mapOptions = {
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    function initialize(){
        geocoder = new google.maps.Geocoder(); // This variables is used to automate zooming and centering in this script
        gmap = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); // Removing this removes the Google Maps layer, but would keep any layers on top of it

        // The Fusion Tables layer variable
        var layer = new google.maps.FusionTablesLayer({
            query: {
                select: locationColumn,
                from: tableId,
                where: condition
            }
        });

        // This code snippet centers and zooms the map to the selected state.
        geocoder.geocode(
            {
                'address':selectedState
            },
            function(results,status){
                var sw = results[0].geometry.viewport.getSouthWest();
                var ne = results[0].geometry.viewport.getNorthEast();
                var bounds = new google.maps.LatLngBounds(sw,ne);

                gmap.fitBounds(bounds)
            }
        );

        layer.setMap(gmap);

        google.maps.event.addDomListener(selectedState,'change',function(){
            updateMap(layer,tableId,locationColumn);
        });         
    }


    // SECTION 3: Find the address once the button is clicked
    $(':button').click(
        function codeAddress(){
            var address = document.getElementById("address").value;

            geocoder.geocode(
                {
                    'address':address
                },
                function(results,status){
                    if(status===google.maps.GeocoderStatus.OK){
                        var addressSW = results[0].geometry.viewport.getSouthWest();
                        var addressNE = results[0].geometry.viewport.getNorthEast();
                        var bounds = new google.maps.LatLngBounds(addressSW,addressNE);

                        gmap.fitBounds(bounds);
                    } else {
                        alert("Couldn't find address for the following reason: " + status + ". Sorry about that. Please try another address.");
                    }
                }
            );
        }
    );


    function updateMap(layer,tableId,locationColumn){
        if (selectedState) {
            tableId = states[selectedState];

            layer.setOptions({
                query: {
                    select: locationColumn,
                    from: tableId,
                    where: condition
                }
            });

            geocoder.geocode(
                {
                    'address':selectedState
                },
                function(results,status){
                    var sw = results[0].geometry.viewport.getSouthWest();
                    var ne = results[0].geometry.viewport.getNorthEast();
                    var bounds = new google.maps.LatLngBounds(sw,ne);

                    gmap.fitBounds(bounds)
                }
            );          
        }
    }

    google.maps.event.addDomListener(window, 'load', initialize);
});

CSS:

#map-canvas {
height: 800px;
width: 1000px;

}

HTML 代码:

<html>
    <head>
        <meta charset="utf8">
        <title>TESTING THE MAPS</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript" src="public/js/script.js"></script>
    </head>
</html>

编辑:指向 HTML、CSS 和 JS 文件的链接---
http://speedy.sh/xRkZF/maptest.html
http://speedy.sh/zSt5p/script.js
http://speedy.sh/uCPxZ/style.css

【问题讨论】:

  • 您发布的代码有什么问题?
  • 当我通过下拉菜单更改状态时,我希望地图在下拉菜单中加载状态的 Fusion Table 地图。现在,它不这样做。
  • 它有什么作用?有什么错误吗?
  • 不,没有错误。如果您打开该页面,则会加载 Alabama 表格,但如果您从下拉列表中选择其他州,则不会发生任何事情。
  • 你能提供一个链接或一个展示问题的jsfiddle吗?

标签: javascript google-maps google-maps-api-3 google-fusion-tables


【解决方案1】:

你已经硬编码了 selectedState,但是你需要在下拉列表改变时获取当前值。

将更改监听器修改为:

google.maps.event.addDomListener(document.getElementById('state-select'),
  '改变',
  功能(){
        updateMap(layer,this.value,locationColumn);
    });

现在传递给 updateMap 的第二个参数将是下拉列表中的选定状态。 还要修改updateMap,把第二个参数的名字改成selectedState:

function updateMap(layer,selectedState,locationColumn){ //功能代码可以保持原样 }

现在一切正常。

【讨论】:

  • 谢谢,@Molle 博士。这行得通,尽管我想知道为什么会这样。我想我只是不熟悉“.addDomListener”的每个参数是什么(而且我认为 Fusion Tables API 参考文档帮助不大)。另外,我想知道“updateMap”中的每个参数是什么意思,以及为什么首先需要它。
猜你喜欢
  • 1970-01-01
  • 2014-03-23
  • 1970-01-01
  • 2012-01-14
  • 2013-02-06
  • 2012-04-24
  • 1970-01-01
  • 2014-10-11
  • 1970-01-01
相关资源
最近更新 更多