Google Maps API V3 - Custom infobox for multiple polygons

☆樱花仙子☆ 提交于 2019-12-12 02:21:20

问题


This is my first time working with the Google Maps API. I have a styled map with multiple polygons that each need their own infobox. The infoboxes need to be styled. My problem is that I can't get the infoboxes to work at all. I've been looking for a solution for days now, I've even tried this http://google-maps-utility-library-v3.googlecode.com/svn/tags/infobox/1.1.9/examples/infobox-basic.html I must obviously be doing something wrong.

Here's my code: http://pastebin.com/M23PPXpn


回答1:


I noticed that attaching an Infobox to a polygon results in this error in infobox.js

Uncaught TypeError: Object #<V> has no method 'getPosition'

It could be because a polygon represents an area, while a marker represents a point. I suggest creating a single invisible marker somewhere inside your polygons to anchor each infobox.

With this idea, I added a infobox to the Australia polygon. The click listener is still created for the polygon, but it opens the infobox tied to the invisible marker.

// ... this is near the end of your code...

australia_new_zealand.setMap(map);
google.maps.event.addListener(australia_new_zealand, 'mouseover', function() {
    this.setOptions({fillColor: "#28d1e9"}); 
});
google.maps.event.addListener(australia_new_zealand, 'mouseout',function(){
 this.setOptions({fillColor: "#06376a"});   
});

      // marker inside the Australia polygon, LatLng was manually defined
      var australia_new_zealand_center = new google.maps.Marker({
        map: map,
        position: new google.maps.LatLng(-24.5271348225978, 134.296875),
        visible: false
      });

      var boxText = document.createElement("div");
      boxText.style.cssText = "border: 1px solid black; margin-top: 8px; background: yellow; padding: 5px;";
      boxText.innerHTML = "City Hall, Sechelt<br>British Columbia<br>Canada";

      var myOptions = {       
        content: boxText,
        maxWidth: 0,
        pixelOffset: new google.maps.Size(-140, 0),
        zIndex: null,
        boxStyle: {  background: "url('tipbox.gif') no-repeat", opacity: 0.75, width: "280px" } ,
        closeBoxMargin: "10px 2px 2px 2px",
        closeBoxURL: "http://www.google.com/intl/en_us/mapfiles/close.gif",
        infoBoxClearance: new google.maps.Size(1, 1),
        isHidden: false,
        pane: "floatPane",
        enableEventPropagation: false
      }

      // listener responds to a click inside polygon
      google.maps.event.addListener(australia_new_zealand, "click", function (e) {
        ib.open(map, australia_new_zealand_center);
      });

      var ib = new InfoBox(myOptions);

//(end) REGION - AUSTRALIA NEW ZEALAND
}  



回答2:


Try this playground with examples Playground



来源:https://stackoverflow.com/questions/9639829/google-maps-api-v3-custom-infobox-for-multiple-polygons

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