Apply mask to Google Map

微笑、不失礼 提交于 2019-12-10 16:45:22

问题


I have a website using the Google-maps Javascript API V3. I would like to highlight the region of interest by placing a semi-transparent color on top of the map with a hole cut in the middle. See image attached.

The position of the cut-out could be anywhere. I am able to calculate the pixel (or lat/lng) coordinates of the cut-out, but what I'm struggling with is how best to create a mask. I've had a look at Google's image tile overlays and polygons but couldn't see a simple way to achieve this. Can anybody offer any pointers on the best way to proceed?


回答1:


Figured it out. Incase anyone else needs to do this ...

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);
    };

}

To use it...

theMask = new MaskClass( referenceToYourMap );   //create mask

theMask.setMask(boundsSouthWest, boundsNorthEast);    //place mask somewhere

theMask.setVisible(false);    //hide mask


来源:https://stackoverflow.com/questions/9555499/apply-mask-to-google-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!