Hover over polygons (showing text) - google-maps

I made a few Polygons on a Google map. Now I want to add a mouseover (and mouseout) to the Polygons, so that when you hover over the Polygon, you get to see the name of the area. and when you mouseout the names goes away (like when you hover over buttons in your browser)
var map;
var infoWindow;
function initialize() {
var myLatLng = new google.maps.LatLng(50.88111111111, 3.889444444444);
var myOptions = {
zoom: 12,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var poly;
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var polyCoords = [
verwissel(3.869506,50.906449),
verwissel(3.869654,50.905664),
verwissel(3.869934,50.904131),
verwissel(3.870310,50.902717),
verwissel(3.870471,50.901559),
];
poly = new google.maps.Polygon({
paths: HerzeleCoords,
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: "#FF0000",
fillOpacity: 0.35
});
google.maps.event.addListener(Poly, "mouseover", function(showtext("polyname"));
google.maps.event.addListener(Poly, "mouseover", function(do not show text anymore);
This is what I think it would look like, but I dont know how it works.

Here's an example: http://jsfiddle.net/tcfwH/304/
Not exactly the same as a browser tooltip, but the text can be styled. I'm using MarkerWithLabel. Each marker is used for the name of its polygon. To toggle multi-line boxes change white-space: nowrap in the CSS. There is also InfoBox as a working option but I find it much more complicated to use than MarkerWithLabel.
The event listeners move the MarkerWithLabel around according to the mouse position:
google.maps.event.addListener(poly, "mousemove", function(event) {
marker.setPosition(event.latLng);
marker.setVisible(true);
});
google.maps.event.addListener(poly, "mouseout", function(event) {
marker.setVisible(false);
});

I haven't tested this in a variety of browsers, but in Chrome it does the trick for me: Call the div containing the map "map_canvas". Also, so that each polygon has its own title, set the property 'sourceName' to the polygon's title.
perimeter.addListener('mouseover',function(){
var map_canvas = document.getElementById("map_canvas");
map_canvas.title = this.sourceName;
});
perimeter.addListener('mouseout',function(){
var map_canvas = document.getElementById("map_canvas");
map_canvas.removeAttribute('title');
});

Related

Adding notes to Shapes overlay in Google Maps

Can we add some notes, a string, while making overlay shapes with google maps API? Like If I draw a circle around my home to indicate High alert area within circle with a note on it, so a person seeing the circle will know quickly, or can I just use color scheme to do this? Please, if you guys have some solution?
Yes you can do it.
Such a thing could be achieved with InfoWindow class, see also InfoWindowOptions object about details what options you can modify
and also check the google documentation sample.
The most important option of the InfoWindowOptions object is content
Type: string|Node
Content to display in the InfoWindow. This can be
an HTML element, a plain-text string, or a string containing HTML. The
InfoWindow will be sized according to the content. To set an explicit
size for the content, set content to be a HTML element with that size.
So let's have a look on how InfoWindow is displayed:
Initialize map (new google.maps.Map)
Initialize InfoWindow
Open the InfoWindow with the open() method
If you want to draw a circle you can use Circle class , see also CircleOptions object to see what options you can adjust. It is easy to draw circles on the map - you just need to instantiate a circle(new google.maps.Circle) and pass the map in the options object.
Check the following demo code and let me know if something is not clear.
function init() {
var center = new google.maps.LatLng(33.53625, -111.92674);
var contentString = '<div id="content">' +
'<div id="bodyContent">' +
'<p>Beware this is my home :)</p>' +
'</div>' +
'</div>';
/*-------------------
MAP
-------------------*/
var map = new google.maps.Map(document.getElementById('map'), {
center: center,
zoom: 13,
scrollwheel: false
});
/*-------------------
CIRCLE
-------------------*/
var circle = new google.maps.Circle({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.4,
map: map,
center: center,
radius: 200
});
/*-------------------
INFO WINDOW
-------------------*/
var infoWindowIsOpen = true;
var infowindow = new google.maps.InfoWindow({
content: contentString,
position: center
});
google.maps.event.addListener(infowindow, 'closeclick', function() {
infoWindowIsOpen = false;
togglePopupButton.innerHTML = "Show Popup"
});
infowindow.open(map);
/*-------------------
TOGGLE INFO WINDOW BUTTON
-------------------*/
var togglePopupButton = document.getElementById('togglePopup');
togglePopupButton.addEventListener('click', function() {
infoWindowIsOpen = !infoWindowIsOpen;
if (infoWindowIsOpen) {
infowindow.open(map);
togglePopupButton.innerHTML = 'Hide Popup';
} else {
infowindow.close();
togglePopupButton.innerHTML = 'Show Popup';
}
});
}
.as-console-wrapper{
display:none !important;
}
<script async defer type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false&callback=init"></script>
<div id="map" style="width:400px;height:150px;float:left"></div>
<button id="togglePopup" style="float:left">Hide Popup</button>

google map, show tooltip on a circle

I know I can make a marker with a tooltip that shows "SOMETHING" like this:
marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,lon),
map: map,
draggable: true,
title:"SOMETHING",
icon: '/public/markers-map/male-2.png'
});
I want to do the same with a circle but title doesn't work.
new google.maps.Circle({
center: new google.maps.LatLng(lat,lon),
radius: 20,
strokeColor: "blue",
strokeOpacity: 1,
title:"SOMETHING",
strokeWeight: 1,
fillColor: "blue",
fillOpacity: 1,
map: map
});
It prints the circle but does not show the message "SOMETHING".
How can I do it? is there another property to get it?
Thanks in advance.
The tooltip is created via the native title-attribute of DOM-elements, but the API doesn't provide any method to access the DOMElement that contains the circle.
A possible workaround may be to use the title-attribute of the map-div instead(set it onmouseover and remove it onmouseout)
//circle is the google.maps.Circle-instance
google.maps.event.addListener(circle,'mouseover',function(){
this.getMap().getDiv().setAttribute('title',this.get('title'));});
google.maps.event.addListener(circle,'mouseout',function(){
this.getMap().getDiv().removeAttribute('title');});
You can also use InfoWindow instead of html title attribute, as the title may not show up always on mouse over. InfoWindow looks pretty good.
var infowindow = new google.maps.InfoWindow({});
var marker = new google.maps.Marker({
map: map
});
Then use same mouseover event mechanism to show the InfoWindow:
google.maps.event.addListener(circle, 'mouseover', function () {
if (typeof this.title !== "undefined") {
marker.setPosition(this.getCenter()); // get circle's center
infowindow.setContent("<b>" + this.title + "</b>"); // set content
infowindow.open(map, marker); // open at marker's location
marker.setVisible(false); // hide the marker
}
});
google.maps.event.addListener(circle, 'mouseout', function () {
infowindow.close();
});
Also we can add event listener direct on google.maps.Circle instance.
Code sample:
//circle is the google.maps.Circle-instance
circle.addListener('mouseover',function(){
this.getMap().getDiv().setAttribute('title',this.get('title'));
});
circle.addListener('mouseout',function(){
this.getMap().getDiv().removeAttribute('title');
});
Just wrote for alternative!

Create Google Map V3 Polygons from XML data

I am trying to create polygons with Data from a XML file. I have never used polygons before and I have studied the Google Map doc examples to get the base features down but the examples don't even work. I tried to merge it with other things I have learned and used to create markers and poly lines but I am missing something and now not only do the polygons not show but the map doesn't even show. I start with base code to display the map and start from there. Once I started adding the code to build the polygon it causes the map to not load. I know I am missing something but I am not sure what exactly since I have never used polygons before.
You can view an example of the XML file I am using for the data. The cords to create the polygons are in a element called "cap:polygon".
http://www.mesquiteweather.net/xml/warnings.xml
Here is the code I have so far...
<script type="text/javascript">
var lineColor = {
"Tornado Warning": "#FF0000",
"Severe Thunderstorm Warning": "#FFFF33",
"Flash Flood Warning": "#00FF00",
};
var infowindow = new google.maps.InfoWindow();
// start here
var thisurl = 'xml/warnings.xml';
function initialize() {
var myLatlng = new google.maps.LatLng(32.775833, -96.796667);
var myOptions = {
panControl: false,
zoom: 5,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.RIGHT_TOP
},
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
function MyLogoControl(controlDiv) {
controlDiv.style.padding = '5px';
var logo = document.createElement('IMG');
logo.src = 'http://www.mesquiteweather.net/images/watermark_MW_GMap.png';
logo.style.cursor = 'pointer';
controlDiv.appendChild(logo);
google.maps.event.addDomListener(logo, 'click', function() {
window.location = 'http://www.mesquiteweather.net';
});
}
var logoControlDiv = document.createElement('DIV');
var logoControl = MyLogoControl(logoControlDiv);
logoControlDiv.index = 0; // used for ordering
map.controls[google.maps.ControlPosition.TOP_LEFT].push(logoControlDiv);
var eventWarnings;
downloadUrl(thisurl, function(data) {
var polygon = data.documentElement.getElementsByTagName("feed");
var warningCoords = new google.maps.LatLng(cap:polygon),
});
// Construct the polygon
eventWarnings = new google.maps.Polygon({
paths: warningCoords,
strokeColor: lineColor[cap:event],
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: lineColor[cap:event],
fillOpacity: 0.35
});
eventWarnings.setMap(map);
}
</script>
I am stuck at this point and not sure what I am missing. If anyone can offer some advice or suggestions that would be great!
-Thanks!
This is incorrect (I would expect it to give you a javascript error):
var warningCoords = new google.maps.LatLng(cap:polygon)
A google.maps.LatLng takes two numbers as an argument. You need to parse the coordinates out of the XML feed, convert them into google.maps.LatLng objects, push them into an array, then provide that array as the paths property in the google.maps.Polygon constructor.

multiple mouse events not triggering

I am using google maps api. I want to have two mouse events ready to trigger at one time. Below is a code snipit.
function initialize() {
var myLatlng = new google.maps.LatLng(37.4419, -122.1419);
var myOptions = {
zoom: 13,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var polyOptions = {
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 3 }
poly = new google.maps.Polyline(polyOptions);
poly.setMap(map);
// Add a listener for the click event
google.maps.event.addListener(map, 'click', addLatLng);
}
function addLatLng(event) {
var path = poly.getPath();
if(!draw)
{
path.push(event.latLng);
path.push(event.latLng);
draw = true;
// Add a new marker at the new plotted point on the polyline.
var marker = new google.maps.Marker({
position: event.latLng,
title: '#' + path.getLength(),
map: map });
google.maps.event.addListener(map, 'mousemove', moveTrackLine);
}
else
{
path.push(event.latLng);
var marker = new google.maps.Marker({
position: event.latLng,
title: '#' + path.getLength(),
map: map });
draw = false;
}
}
function moveTrackLine(event) {
var path = poly.getPath();
// replace the old point with a new one to update the track
path.pop();
path.push(event.latLng);
}
When I click on the map the first time I see a marker placed on the map. Then when the mouse is moved I see the polyline update and follow my curser correctly. Next if I click on the map I do not see a marker placed on the map nor do I ever go into the addLatLng function. Thanks in advance for your help and time.
-tim
set the clickable-option of the polyline to false.
The issue: As soon as the mousemove-event is applied to the map, you will not be able to click on the map anymore, because below the mouse-cursor is always the polyline.
When you set the clickable-option of the polyline to false, the polyline does not respond to mouse-events and the click-event will be passed to the map.

How to Draw a Rectangle on Google Map using Google Map API

i am new to stackoveflow, can any one help me to how to draw a rectangle using google api v3, i went through some examples on google i got the below code,
function initialize() {
var coachella = new google.maps.LatLng(33.6803003, -116.173894);
var rectangle;
var myOptions = {
zoom: 11,
center: coachella,
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
rectangle = new google.maps.Rectangle();
google.maps.event.addListener(map, 'zoom_changed', function() {
// Get the current bounds, which reflect the bounds before the zoom.
var rectOptions = {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: "#FF0000",
fillOpacity: 0.35,
map: map,
bounds: map.getBounds()
};
rectangle.setOptions(rectOptions);
});
}
But, it functions on zoom event(zoom chanages),i want simple with out event,please help me
map.getBounds() may return not the desired bounds when called to early(immediately after the instantiating of the map).
You may use tilesloaded instead
google.maps.event.addListenerOnce(map, 'tilesloaded', function() {
/*your code*/
});