有没有办法使叠加对象不可点击?

时间:2012-12-25 19:52:19

标签: google-maps-api-3

我正在研究我的这个项目,需要通过点击来定义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>

fiddle

1 个答案:

答案 0 :(得分:8)

设置{clickable: false} in the CircleOptions

    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