0

已经成功地遵循了一些 Fusion Map 示例,在这里得到了一些人的帮助。我正在尝试做一些我能够找到的例子......

我的地图包含 3 个图层,但我只想一次显示 2 个图层。因此,我在底部添加了一个下拉框,以便您可以在 2 个地图之间切换。我添加了一个变量“SecondLayerMap”,我认为(我至少尝试过)让下拉框更改这个变量。直到 3 周前我才写过任何 Java(或任何其他编程语言),所以这是一个陡峭的学习曲线!

[我见过一些人在类似情况下使用的一种方法是使用 1 个融合表,但在不同列的数据之间切换。我认为我不能这样做,因为每个表中的地理数据不同,我不希望它们同时显示。]

下一步将是使用地图中的开关更改其中一个图例,但在解决那个问题之前,我将从这个问题中学到什么...

谢谢你的帮助。

    <meta charset="UTF-8">
    <title>Smithfield Foods UK</title>
    <link rel="stylesheet" type="text/css" media="all" href="FusionMapTemplate.css" />

    <script type="text/javascript"
        src="http://maps.google.com/maps/api/js?sensor=false"></script>

    <script type="text/javascript">

        function initialize() {
            var defaultZoom = 10;
            var defaultCenter = new google.maps.LatLng(52.6500, 1.2800);
            var locationColumn = 'geometry';
            var geocoder = new google.maps.Geocoder();
            var SecondLayerMap = '1RrCcRC-1vU0bfHQJTQWqToR-vllSsz9iKnI5WEk'

            var map = new google.maps.Map(document.getElementById('map-canvas'), {
                center: defaultCenter,
                zoom: defaultZoom,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            });

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

            // Initialize the first layer
            var FirstLayer = new google.maps.FusionTablesLayer({
                query: {
                select: 'geometry',
                from: '1hpGzmMBg8bDgPOGrAXvc0_QVLSBqQ0O5vpLbfUE'
                },
                map: map,
                styleId: 3,
                templateId: 5,
                suppressInfoWindows: true
            });
            google.maps.event.addListener(FirstLayer, 'click', function(e) {windowControl(e, infoWindow, map);
            });

            // Initialize the second layer
            var SecondLayer = new google.maps.FusionTablesLayer({
                query: {
                    select: 'PostCode',
                    from: SecondLayerMap
                },
                map: map,
                styleId: 2,
                templateId: 2,
                suppressInfoWindows: true
            });

            google.maps.event.addDomListener(document.getElementById('store'), 'change', function() {
                var SecondLayerMap = this.value;
                SecondLayer = new google.maps.FusionTablesLayer({
                    query: {
                        select: 'Postcode',
                        from: SecondLayerMap
                    }
                });

            });


            google.maps.event.addListener(SecondLayer, 'click', function(e) {windowControl(e, infoWindow, map);
            });

        var legend = document.createElement('div');
        legend.id = 'legend';
        var content = [];
        content.push('<h3>Density of Polish speaking population</h3>');
        content.push('<p><div class="color red1"></div>=>2%<4%');
        content.push('<p><div class="color red2"></div>=>4%<6%');
        content.push('<p><div class="color red3"></div>=>6%<10%');
        content.push('<p><div class="color red4"></div>=>10%<15%');
        content.push('<p><div class="color red5"></div>=>15%<20%')
        content.push('<p><img src="Smithfield Black.png" alt="Smithfield Logo" width ="120px">');
        legend.innerHTML = content.join('');
        legend.index = 1;
        map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(legend);

        var legend2 = document.createElement('div');
        legend2.id = 'legend2';
        var content2 = [];
        content2.push("<h3>Smithfield Food's sales in Asda Stores</h3>");
        content2.push('<p><img src="red-dot.png"><£1,000');
        content2.push('<p><img src="yellow-dot.png">=>£1,000<£20,000');
        content2.push('<p><img src="green-dot.png">=>£20,000<£40,000');

        legend2.innerHTML = content2.join('');
        legend2.index = 1;
        map.controls[google.maps.ControlPosition.RIGHT_TOP].push(legend2);

        var zoomToAddress = function() {
            var address = document.getElementById('address').value;
            geocoder.geocode({
                address: address
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);
                    map.setZoom(10);
                } else {
                    window.alert('Address could not be geocoded: ' + status);
                }
            });
        };


        google.maps.event.addDomListener(document.getElementById('search'), 'click', zoomToAddress);
        google.maps.event.addDomListener(window, 'keypress', function(e) {
            if (e.keyCode ==13) {
            zoomToAddress();
        }
        });

        google.maps.event.addDomListener(document.getElementById('reset'), 'click', function() {
            map.setCenter(defaultCenter);
            map.setZoom(defaultZoom);
            layer.setOptions({
                query: {
                    select: 'geometry',
                    from: '1hpGzmMBg8bDgPOGrAXvc0_QVLSBqQ0O5vpLbfUE'
                }
            });
        });

        }


        // Open the info window at the clicked location
        function windowControl(e, infoWindow, map) {
            infoWindow.setOptions({
                content: e.infoWindowHtml,
                position: e.latLng,
                pixelOffset: e.pixelOffset
            });
            infoWindow.open(map);
        }

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

    </script>
</head>

<body>
    <div id="map-canvas"></div>
    <div>
        <label>Enter an address:</label>
        <input type="text" id="address" value="Leeds">
        <input type="button" id="search" value="Search">
        <input type="button" id="reset" value="Reset">
    </div>
    <div>
        <select id="store">
            <option value ="1RrCcRC-1vU0bfHQJTQWqToR-vllSsz9iKnI5WEk">Store A</option>
            <option value ="1QX6QXhAiHXXAcS96RSAmE1Caj8tWebc6d-1_Tjk">Store B</option>
        </select>

</body>

4

1 回答 1

1

用于setOptions()将查询应用于图层:

    google.maps.event.addDomListener(document.getElementById('store'), 
        'change', 
        function() {
        var SecondLayerMap = this.value;
        SecondLayer.setOptions({
            query: {
                select: 'Postcode',
                from: SecondLayerMap
            }
        });
于 2013-03-08T12:39:25.977 回答