Google Maps - Attaching InfoWindows to polygons in array

后端 未结 3 740
半阙折子戏
半阙折子戏 2021-01-21 07:46

I\'ve been banging my head against the wall all morning with this one. I an creating an array of polygons, and want to associate some data in each one that will show in an info

3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-21 08:15

    I can provide some useful suggestions about your question.

    Firstly, you should know the expression about the method 'infowindow.open(map, anchor?:MVCObject)'. As the google api v3 says: In the core API, the only anchor is the Marker class. Polygon class does not match the condition, however, we can achieve in another way.

    var polygon = new google.maps.Polygon({
        paths: PGpoints,   //The PGpoints is the collection of points around this polygon.
        map: map,
        strokeColor: colory,
        strokeOpacity: 0.6,
        strokeWeight: 1,
        fillColor: colory,
        fillOpacity: 0.35       
    });
    
    polygon.set("Info", idy);  // Set some attributes for adding extra information into this polygon.   
    
    google.maps.event.addListener(polygon, 'click', function() {
        var infoWindow = new google.maps.InfoWindow();
        infoWindow.setContent("Information : " + polygon.get("Info"));
    
        // 'laty,lngy' is the location of one point in PGpoints, which can be chosen as you wish
        infoWindow.setPosition(new google.maps.LatLng(laty,lngy));     
        infoWindow.open(map);
    });
    

    The code above has passed test, you can use it directly. Then, if you click one polygon, infoWindow would appear above the map.

提交回复
热议问题