2

我正在处理我的这个项目,该项目需要通过单击来定义 lat/lng 元素,最后找到了一种方法,但刚刚发现,已经预定义的元素会干扰下面源定义的新覆盖元素。所以我看了又看,搜索,谷歌搜索,但找不到任何有用的信息:有没有办法让谷歌地图覆盖不可点击?

我正在使用自定义函数来获取单击事件的纬度和经度并放置预定义的圆形覆盖对象。但是,如果我已经预定义了覆盖元素,则无法单击它们顶部来设置新的覆盖元素。即,我需要使它们不可交互或不可点击,或者只是将它们设置在不同的层上,这样它们就不会干扰新元素的点击事件。

这是我使用的 JS:

<script type="text/javascript">
    var map;
    var markersArray = []; //the array for the newly defined objects

    function initMap()
    {
        var latlng = new google.maps.LatLng(41, 29);
        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        // add a click event handler to the map object
        google.maps.event.addListener(map, "click", function(event)
        {
            // place a marker
            placeMarker(event.latLng);

        });

            // I've predefined a couple of markers just to see how it works with already defined elements and discovered this interference that I mentioned above   
        var mar1 = new google.maps.LatLng(40.9653, 29.3705);
        var marker1 = new google.maps.Circle({
        center: mar1,
        radius: 2500,
        fillColor: "#FF0000",
        strokeWeight: 0,
        fillOpacity: 0.35,
        map: map
        });
        var mar2 = new google.maps.LatLng(40.9664, 29.3252);
        var marker2 = new google.maps.Circle({
        center: mar2,
        radius: 2500,
        fillColor: "#FF0000",
        strokeWeight: 0,
        fillOpacity: 0.35,
        map: map
        });
    marker1.setMap(map);
    marker2.setMap(map);

    }

    function placeMarker(location) {
        // first remove all new markers if there are any, so that we define one new at a time
        deleteOverlays();

        var new_marker = new google.maps.Circle({
            center: location,
            radius: 2500,
            strokeColor: "#FF0000",
            strokeOpacity: 0.8,
            strokeWeight: 0,
            fillColor: "#FF0000",
            fillOpacity: 0.35,
            //position: location, 
            map: map
        });

        // add marker in markers array
        markersArray.push(new_marker);

    }

    // Deletes all markers in the array by removing references to them
    function deleteOverlays() {
        if (markersArray) {
            for (i in markersArray) {
                markersArray[i].setMap(null);
            }
        markersArray.length = 0;
        }
    }

</script>

小提琴

4

1 回答 1

8

在 CircleOptions 中设置{clickable: false} 。

    var new_marker = new google.maps.Circle({
        center: location,
        radius: 2500,
        strokeColor: "#FF0000",
        strokeOpacity: 0.8,
        strokeWeight: 0,
        fillColor: "#FF0000",
        fillOpacity: 0.35,
        clickable: false,     // <=====================
        map: map
    });

Modified jsfiddle

于 2012-12-25T21:00:47.530 回答