views:

32

answers:

1

Hi folks,

I'm trying to show and remove polygons onto a google map - using v3 of the API. In my javascript, i've already got an MVCArray of some custom Lat-Longs.

What I'm stuck trying to figure out is how to add these polys and then (based upon some other javascript event / user experience feedback .. like clicking on a poly (that has been rendered) ... that poly will be removed.

Can someone help? Code or links to examples? I'm struggling to find some examples .. most of them usually goto some v2 code..

cheers :)

+1  A: 

In the API docs, there are a couple of simple examples of adding a polygon to a map. Here's the initialize() function from the simple Bermuda Triangle example with the addition of adding an event listener to remove the polygon when clicked.

function initialize() {
  var myLatLng = new google.maps.LatLng(24.886436490787712, -70.2685546875);
  var myOptions = {
    zoom: 5,
    center: myLatLng,
    mapTypeId: google.maps.MapTypeId.TERRAIN
  };

  var bermudaTriangle;

  var map = new google.maps.Map(document.getElementById("map_canvas"),
      myOptions);

  var triangleCoords = [
      new google.maps.LatLng(25.774252, -80.190262),
      new google.maps.LatLng(18.466465, -66.118292),
      new google.maps.LatLng(32.321384, -64.75737),
      new google.maps.LatLng(25.774252, -80.190262)
  ];

  // Construct the polygon
  bermudaTriangle = new google.maps.Polygon({
    paths: triangleCoords,
    strokeColor: "#FF0000",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#FF0000",
    fillOpacity: 0.35
  });

  bermudaTriangle.setMap(map);

  // add an event listener
  google.maps.event.addListener(bermudaTriangle, 'click', function() {
      this.setMap(null);
  });

}
Mark
AHH! so the trick was to make sure the google.maps.Polygon _instance_ is global .. so u can then reference the same instance to remove it .. instead of trying to find the instance on the map, etc. Gotcha :)
Pure.Krome