Open InfoWindow for each polygon google maps V3

前端 未结 2 500
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 05:35

Hope someone can help me with this issue. I\'m trying to open an info windows on click for each polygon that my users created. I used the same code for a marker and works w

2条回答
  •  抹茶落季
    2020-12-30 06:26

    You can't use the same form of InfoWindow.open for a polygon as you use for a marker (there is no marker to pass in).

    From the documentation

    open(map?:Map|StreetViewPanorama, anchor?:MVCObject)

    Return Value: None

    Opens this InfoWindow on the given map. Optionally, an InfoWindow can be associated with an anchor. In the core API, the only anchor is the Marker class. However, an anchor can be any MVCObject that exposes a LatLng position property and optionally a Point anchorPoint property for calculating the pixelOffset (see InfoWindowOptions). The anchorPoint is the offset from the anchor's position to the tip of the InfoWindow.)

    You need to specifically set the place you want it to open when you call the open method (the latlng of the click is usually a good choice) with InfoWindow.setPosition().

    Example

    code snippet:

    var infowindow = new google.maps.InfoWindow({
      size: new google.maps.Size(150, 50)
    });
    
    
    function initialize() {
      var geolib = google.maps.geometry.spherical;
      var myOptions = {
        zoom: 20,
        center: new google.maps.LatLng(32.738158, -117.14874),
        mapTypeControl: true,
        mapTypeControlOptions: {
          style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
        },
        navigationControl: true,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    
      google.maps.event.addListener(map, 'click', function() {
        infowindow.close();
      });
      bounds = new google.maps.LatLngBounds();
    
      var polygon1 = new google.maps.Polygon({
        map: map,
        path: [geolib.computeOffset(new google.maps.LatLng(32.737355, -117.148719), 100, 0),
          geolib.computeOffset(new google.maps.LatLng(32.737355, -117.148719), 100, 120),
          geolib.computeOffset(new google.maps.LatLng(32.737355, -117.148719), 100, -120)
        ],
        name: "polygon1"
      });
      google.maps.event.addListener(polygon1, 'click', function(event) {
        var contentString = "name:" + this.name + "
    " + event.latLng.toUrlValue(6); infowindow.setContent(contentString); infowindow.setPosition(event.latLng); infowindow.open(map); }); for (var i = 0; i < polygon1.getPath().getLength(); i++) { bounds.extend(polygon1.getPath().getAt(i)); } var polygon2 = new google.maps.Polygon({ map: map, path: [geolib.computeOffset(new google.maps.LatLng(32.739341, -117.148912), 90, 180), geolib.computeOffset(new google.maps.LatLng(32.739341, -117.148912), 90, 60), geolib.computeOffset(new google.maps.LatLng(32.739341, -117.148912), 90, -60) ], name: "polygon2" }); google.maps.event.addListener(polygon2, 'click', function(event) { var contentString = "name:" + this.name + "
    " + event.latLng.toUrlValue(6); infowindow.setContent(contentString); infowindow.setPosition(event.latLng); infowindow.open(map); }); for (var i = 0; i < polygon2.getPath().getLength(); i++) { bounds.extend(polygon2.getPath().getAt(i)); } map.fitBounds(bounds); } google.maps.event.addDomListener(window, 'load', initialize); function createClickablePoly(poly, html, label, point) { gpolys.push(poly); if (!point && poly.getPath && poly.getPath().getLength && (poly.getPath().getLength > 0) && poly.getPath().getAt(0)) { point = poly.getPath().getAt(0); } var poly_num = gpolys.length - 1; if (!html) { html = ""; } else { html += "
    "; } var length = poly.Distance(); if (length > 1000) { html += "length=" + poly.Distance().toFixed(3) / 1000 + " km"; } else { html += "length=" + poly.Distance().toFixed(3) + " meters"; } for (var i = 0; i < poly.getPath().getLength(); i++) { html += "
    poly[" + poly_num + "][" + i + "]=" + poly.getPath().getAt(i); } html += "
    Area: " + poly.Area() + " sq meters"; // html += poly.getLength().toFixed(2)+" m; "+(poly.getLength()*3.2808399).toFixed(2)+" ft; "; // html += (poly.getLength()*0.000621371192).toFixed(2)+" miles"; var contentString = html; google.maps.event.addListener(poly, 'click', function(event) { infowindow.setContent(contentString); if (event) { point = event.latLng; } infowindow.setPosition(point); infowindow.open(map); // map.openInfoWindowHtml(point,html); }); if (!label) { label = "polyline #" + poly_num; } label = "" + label + ""; // add a line to the sidebar html // side_bar_html += '' + label + '
    '; }
    body,
    html {
      height: 100%;
      width: 100%;
    }
    
    

提交回复
热议问题