【问题标题】:Apply mask to Google Map将掩码应用于谷歌地图
【发布时间】:2012-03-04 14:05:10
【问题描述】:

我有一个使用 Google-maps Javascript API V3 的网站。 我想通过在地图顶部放置半透明颜色并在中间切一个洞来突出显示感兴趣的区域。见附图。

切口的位置可以在任何地方。我能够计算出切口的像素(或纬度/经度)坐标,但我正在努力解决的是如何最好地创建蒙版。我查看了 Google 的图像平铺叠加层和多边形,但看不到实现此目的的简单方法。任何人都可以提供有关最佳方式的任何指示吗?

【问题讨论】:

    标签: google-maps google-maps-api-3 overlay


    【解决方案1】:

    想通了。以防其他人需要这样做...

    function MaskClass( map ){
    
        //constants
        var MAP_MAX_LAT = 85;
        var MAP_MIN_LAT = -85;
        var MAP_MAX_LNG = 179;
        var MAP_MIN_LNG = -179;
    
        // Object initialisation - create 4 rectangles to mask off the area
        var rectangleInitialisationOptions = {
            map: map,
            fillColor: "#F00",
            fillOpacity: 0.3,
            strokeOpacity: 0,
            clickable: false
            };
    
        this.rectangle1 = new google.maps.Rectangle(rectangleInitialisationOptions);
        this.rectangle2 = new google.maps.Rectangle(rectangleInitialisationOptions);
        this.rectangle3 = new google.maps.Rectangle(rectangleInitialisationOptions);
        this.rectangle4 = new google.maps.Rectangle(rectangleInitialisationOptions);
    
        // Method to place the cut-out   
        this.setMask = function(boundsSouthWest, boundsNorthEast){
    
            var swLat = boundsSouthWest.lat();
            var swLng = boundsSouthWest.lng();
            var neLat = boundsNorthEast.lat();
            var neLng = boundsNorthEast.lng();
    
            this.rectangle1.setBounds(
                new google.maps.LatLngBounds(
                    new google.maps.LatLng(neLat, MAP_MIN_LNG),
                    new google.maps.LatLng(MAP_MAX_LAT, MAP_MAX_LNG)));
    
            this.rectangle2.setBounds(
                new google.maps.LatLngBounds(
                    new google.maps.LatLng(MAP_MIN_LAT, MAP_MIN_LNG),
                    new google.maps.LatLng(swLat, MAP_MAX_LNG)));
    
            this.rectangle3.setBounds(
                new google.maps.LatLngBounds(
                    new google.maps.LatLng(swLat, MAP_MIN_LNG),
                    new google.maps.LatLng(neLat, swLng)));
    
            this.rectangle4.setBounds(
                new google.maps.LatLngBounds(
                    new google.maps.LatLng(swLat, neLng),
                    new google.maps.LatLng(neLat, MAP_MAX_LNG)));
    
            this.setVisible(true);
        };
    
        // Method to show/hide the mask
        this.setVisible = function(visibility){
            this.rectangle1.setVisible(visibility);
            this.rectangle2.setVisible(visibility);
            this.rectangle3.setVisible(visibility);
            this.rectangle4.setVisible(visibility);
        };
    
    }
    

    要使用它...

    theMask = new MaskClass( referenceToYourMap );   //create mask
    
    theMask.setMask(boundsSouthWest, boundsNorthEast);    //place mask somewhere
    
    theMask.setVisible(false);    //hide mask
    

    【讨论】:

    • 我知道这已经晚了,但我可以正常工作。我正在尝试使面具可拖动,这会很有趣:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2017-05-17
    • 2014-05-19
    相关资源
    最近更新 更多