Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:38:54+00:00 2026-06-13T06:38:54+00:00

Hope someone can help me with this issue. I’m trying to open an info

  • 0

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 well but i couldn’t make it work for each polygon.

Any thoughts on how to solve this problem?

var contentString = '<div id="content">'+
    '<div id="siteNotice">'+
    '</div>'+
    '<h2>Test</h2>'+
    '</div>';

var infowindow = new google.maps.InfoWindow({
    content: contentString
});

// Show Areas

<?php foreach ($field->result() as $f):?>

// Create an array with the coordanates of each area

var field<?=$f->id?>Coords = [
    <?php $latlng=$this->resources_data->field_latlng($f->id);?>
    <?php foreach ($latlng->result() as $point):?>
    new google.maps.LatLng(<?=$point->lat?>, <?=$point->lng?>),
    <?php endforeach;?>
];

// Create a polygon with the points of the area

var area<?=$f->id?>=new google.maps.Polygon({
    paths: area<?=$f->id?>Coords,
    strokeColor: '#FF0000',
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: '#FF0000',
    fillOpacity: 0.35
});

// Add the area to the map.

area<?=$f->id?>.setMap(map);

google.maps.event.addListener(area<?=$f->id?>,'click',function(){
    infowindow.open(map,area<?=$f->id?>)
});

<?php endforeach;?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:38:56+00:00Added an answer on June 13, 2026 at 6:38 am

    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 + "<br>" + 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 + "<br>" + 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 += "<br>";
      }
      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 += "<br>poly[" + poly_num + "][" + i + "]=" + poly.getPath().getAt(i);
      }
      html += "<br>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 = "<a href='javascript:google.maps.event.trigger(gpolys[" + poly_num + "],\"click\");'>" + label + "</a>";
      // add a line to the sidebar html
      //  side_bar_html += '<input type="checkbox" id="poly'+poly_num+'" checked="checked" onclick="togglePoly('+poly_num+');">' + label + '<br />';
    }
    body,
    html {
      height: 100%;
      width: 100%;
    }
    <script src="https://maps.google.com/maps/api/js?libraries=geometry&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <table border="1" style="height:100%; width:100%">
      <tr>
        <td>
          <div id="map_canvas" style="width:100%; height:100%"></div>
        </td>
        <td width="200">
          <div id="report"></div>
        </td>
      </tr>
    </table>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hope someone can help me out of this issue. Im trying to get data
I hope someone can help me with this Drupal 7 issue. I noticed that
I am having a JQuery/ASP issue that I hope someone can help me with.
I hope someone can help... This issue has been discussed here and I have
Hope someone can help me here? Having an issue getting text that's located inside
I need some help and hope someone can help me with this issue: hturl
Hello all I hope someone can help me resolve this issue.. I'm curious as
I really hope someone can help me with this. Basically i have an issue
I hope someone can help with this issue. I have a popup iframe using
I hope that someone can help me with an issue related to binding a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.