0

我从这里关注了搜索和缩放示例:https ://developers.google.com/fusiontables/docs/samples/search_and_zoom

我已经设法让“重置”按钮工作,但“搜索”按钮不起作用。我想知道是不是因为我使用了 2 层,但如果是这种情况,我不知道如何通过 java 脚本进行更正。任何帮助表示赞赏。谢谢。

<html> 
<head>

    <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 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: '1RrCcRC-1vU0bfHQJTQWqToR-vllSsz9iKnI5WEk'
                },
                map: map,
                styleId: 2,
                templateId: 2,
                suppressInfoWindows: true
            });
            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>

</body>

</html>
4

1 回答 1

0

我收到“地理编码器未定义”错误”。因为它没有定义。

解决它的一种方法是将其添加到全局范围(就在您的初始化函数之前):

var geocoder = new google.maps.Geocoder();

或者您可以按照您使用的示例中的方式进行操作,它位于初始化函数内部。

工作示例

于 2013-03-05T15:36:41.663 回答