I’m using the Google Maps javascript V3 API and I’m having problems with setting the visibility of a rectangle overlay. Basically, I have a click listener, and inside the listener I have this code:
var neLat = containmentCenter.lat()+ydiff;
var neLng = containmentCenter.lng()+xdiff;
var swLat = containmentCenter.lat()-ydiff;
var swLng = containmentCenter.lng()-xdiff;
nebound = google.maps.LatLng(neLat, neLng);
swbound = google.maps.LatLng(swLat, swLng);
crbounds = google.maps.LatLngBounds(swbound, nebound);
var containRectangleOptions = {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map,
bounds: crbounds,
editable: true,
visible: true
};
containRectangle.setMap(map);
containRectangle.setOptions(containRectangleOptions);
containRectangle.setVisible(true);
alert("Center: ("+containmentCenter.lat()+","+containmentCenter.lng()+")\nY:
"+ydiff+"\nX: "+xdiff+"\nVisible: "+containRectangle.getVisible());
Where containRectangle is a Rectangle overlay which I define inside of initialize() and above the listener with
containRectangle = new google.maps.Rectangle(defaultRectangleOptions);
defaultRectangleOptions are the exact same as containRectangleOptions except the bounds are map.getBounds() and visible is set to false.
The problem is no rectangle is visible when I click, regardless of the fact that the alert() I have shows the correct coordinates and also correctly says that containRectangle.getVisible() is true. I’ve also tried having containRectangle be visible in the defaultRectangleOptions but it still doesn’t show up. I also have some almost identical code for displaying a circle where I click, and that works exactly correctly, but the rectangle never shows up.
I solved my own problem. I ended up being an error in the way I was initializing the bounds objects. Using the
newoperator fixed everything.