Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图国家鼠标点击(FusionTables)_Javascript_Google Maps_Google Fusion Tables - Fatal编程技术网

Javascript 谷歌地图国家鼠标点击(FusionTables)

Javascript 谷歌地图国家鼠标点击(FusionTables),javascript,google-maps,google-fusion-tables,Javascript,Google Maps,Google Fusion Tables,这段代码在国家上绘制多边形,当我的光标指向一个国家时,多边形的颜色正在改变,但我在eventlistener中遇到问题,当我单击一个国家时,总是提醒姓氏我的错误是什么thaks寻求帮助 <script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script> <script type="text/javascript"> va

这段代码在国家上绘制多边形,当我的光标指向一个国家时,多边形的颜色正在改变,但我在eventlistener中遇到问题,当我单击一个国家时,总是提醒姓氏我的错误是什么thaks寻求帮助

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

<script type="text/javascript">
    var colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00'];
    var map;

    function initialize() {
        var myOptions = {
            zoom: 2,
            center: new google.maps.LatLng(10, 0),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById('map-canvas'),
            myOptions);

        // Initialize JSONP request
        var script = document.createElement('script');
        var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
        url.push('sql=');
        var query = 'SELECT name, kml_4326 FROM ' +
            '1foc3xO9DyfSIF6ofvN0kp2bxSfSeKog5FbdWdQ';
        var encodedQuery = encodeURIComponent(query);
        url.push(encodedQuery);
        url.push('&callback=drawMap');
        url.push('&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ');
        script.src = url.join('');
        var body = document.getElementsByTagName('body')[0];
        body.appendChild(script);
    }

    function drawMap(data) {
        var rows = data['rows'];
        var country= new Array();
        for (var i in rows) {
            if (rows[i][0] != 'Antarctica') {
                var newCoordinates = [];
                var geometries = rows[i][1]['geometries'];
                if (geometries) {
                    for (var j in geometries) {
                        newCoordinates.push(constructNewCoordinates(geometries[j]));
                    }
                } else {
                    newCoordinates = constructNewCoordinates(rows[i][1]['geometry']);
                }
                var randomnumber = Math.floor(Math.random() * 4);
                 country[i] = new google.maps.Polygon({
                    paths: newCoordinates,
                    strokeColor: colors[0],
                    strokeOpacity: 0,
                    strokeWeight: 1,
                    fillColor: colors[0],
                    fillOpacity: 0
                });
                google.maps.event.addListener(country[i], 'mouseover', function () {
                    this.setOptions({ fillOpacity: 0.2 });
                });
                google.maps.event.addListener(country[i], 'mouseout', function () {
                    this.setOptions({ fillOpacity: 0 });
                });
                google.maps.event.addListener(country[i], "click", function (event) {
                    alert(rows[i][0]);
                });
                country[i].setMap(map);
            }

        }
    }

    function constructNewCoordinates(polygon) {
        var newCoordinates = [];
        var coordinates = polygon['coordinates'][0];
        for (var i in coordinates) {
            newCoordinates.push(
                new google.maps.LatLng(coordinates[i][1], coordinates[i][0]));
        }
        return newCoordinates;
    }

    google.maps.event.addDomListener(window, 'load', initialize);
</script>

您的单击侦听器总是向相同的数据发出警报:

            google.maps.event.addListener(country[i], "click", function (event) {
                alert(rows[i][0]);
            });
行[i][0]将始终是相同的值,因为当循环退出时,我将始终引用最后一个值

修复它的一种方法是使用、创建一个函数:

createPolygonClidkHandler(polygon, text) {
   google.maps.event.addListener(polygon, "click", function (event) {
     alert(text);
   });
然后使用该函数保持多边形及其关联文本的闭合,如下所示:

function drawMap(data) {
    var rows = data['rows'];
    var country= new Array();
    for (var i in rows) {
        if (rows[i][0] != 'Antarctica') {
            var newCoordinates = [];
            var geometries = rows[i][1]['geometries'];
            if (geometries) {
                for (var j in geometries) {
                    newCoordinates.push(constructNewCoordinates(geometries[j]));
                }
            } else {
                newCoordinates = constructNewCoordinates(rows[i][1]['geometry']);
            }
            var randomnumber = Math.floor(Math.random() * 4);
             country[i] = new google.maps.Polygon({
                paths: newCoordinates,
                strokeColor: colors[0],
                strokeOpacity: 0,
                strokeWeight: 1,
                fillColor: colors[0],
                fillOpacity: 0
            });
            google.maps.event.addListener(country[i], 'mouseover', function () {
                this.setOptions({ fillOpacity: 0.2 });
            });
            google.maps.event.addListener(country[i], 'mouseout', function () {
                this.setOptions({ fillOpacity: 0 });
            });
            createPolygonClidkHandler(country[i], rows[i][0]);
            country[i].setMap(map);
        }

    }
}

可能的副本。您的单击侦听器始终会发出相同值的警报。您应该使用鼠标盖或函数闭包执行相同的操作。抱歉,可能重复我粘贴了错误的代码,通常是alertrows[i][0];我更新了问题,但没有改变答案。我将指向数组中的最后一个条目。这就是发生的情况:但我不明白为什么我要修复它,我需要在单击时提醒每个国家的名称