Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google maps v3 adding an info window to a circle

Is there a way to add a infoWindow to a circle created by google.maps.Circle

something like this

var circ = new google.maps.Circle({
            center:latlng,
            clickable:false,
            fillColor:colors[count],
            fillOpacity:0.3, 
            map:map,
            radius:radius,
            strokeColor:colors[count],
            strokeOpacity:0.3});

and

//create info window
var infoWindow= new google.maps.InfoWindow({
    content: "Your info here"
    });

//add a click event to the circle
google.maps.event.addListener(circ, 'click', function(){
//call  the infoWindow
infoWindow.open(map, circ);
}); 

or alternatively is there a way to create an invisible marker at the center of the circle that can be clicked on to access an infoWindow

like image 1000
trs Avatar asked Jul 05 '11 14:07

trs


People also ask

How do I use circles on Google Maps?

If you're using CalcMaps, click on Draw a circle and add the circle on the map area you're interested in. Use can then use the drop-down menu to select the radius type you want to use. The tool automatically calculates the circle area as well.

How do I draw multiple circles on Google Maps?

Click anywhere on the map to create a circle. You may continue clicking outside the circles to create more circles. You can reposition (drag) the circles with your mouse by clicking+holding+moving. In the following example, the circles are draggable and editable.

How do I draw a circle in Google Earth Web?

Click (click, don't drag) on the map at the center point of your circle (eg: Vancouver) Move your mouse until the circle radius is the distance you want (eg: 750km) Click the map again to complete the circle. In the Ruler window, click the "Save" button.


2 Answers

you can have info window for your circle overlay. But you have to slightly adjust your code.

First, it is necessary to set clickable=true for your Circle overlay (otherwise click events on the circle are not processed).

Then you have to change the code of the click listener. Putting circle as a parameter to function open() has no effect (Circle is not the proper kind of MVCObject, for explanation read documentation of InfoWindow.open() function). To display the info window you have to provide its position - e.g. position of the click event, center of the circle, ....

The code is then

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(ev.latLng);
    infoWindow.open(map);
});

or

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(circ.getCenter());
    infoWindow.open(map);
});

Answer to your comment: You can create a trick with an invisible marker (just put fully transparent image as the marker icon), but I would prefer solution with Circle overlay.

like image 74
Tomik Avatar answered Sep 21 '22 22:09

Tomik


to set info window at where mouse clicked

google.maps.event.addListener(circ, 'click', function(ev){
    infoWindow.setPosition(ev.latLng); //<-- ev matches what you put ^ (mouse event)
    infoWindow.open(map);
});
like image 45
Doug Avatar answered Sep 21 '22 22:09

Doug