Tooltip over a Polygon in Google Maps - google-maps

I have polygons for various region and states in my application. Markers implement tooltip by taking the title attribute. On mouseover and mouseout over a polygon events can be fired. How do I create a tooltip that looks like the tooltip that is implemented for a marker.
Edit-1: Adding the code used to create the polygon and attach the handlers to show/hide tooltips.
function addPolygon(points) {
var polygon = new google.maps.Polygon({
paths: points,
strokeColor: " #FFFFFF",
strokeOpacity: 0.15,
strokeWeight: 1.5,
fillColor: "#99ff66",
fillOpacity: 0.14
});
var tooltip = document.createElement('div');
tooltip.innerHTML = "Alok";
google.maps.event.addListener(polygon,'mouseover',function(){
tooltip.style.visibility = 'visible';
});
google.maps.event.addListener(polygon,'mouseout',function(){
tooltip.style.visibility = 'hidden';
});
polygon.setMap(map);
}

There's actually a neat trick to work around this (strange) limitation in Google Maps. When moving your mouse over a data item, you can just add a tooltip to the map's <div>. It will be added at the location where your mouse pointer currently is - right over the item!
map.data.addListener('mouseover', mouseOverDataItem);
map.data.addListener('mouseout', mouseOutOfDataItem);
...
function mouseOverDataItem(mouseEvent) {
const titleText = mouseEvent.feature.getProperty('propContainingTooltipText');
if (titleText) {
map.getDiv().setAttribute('title', titleText);
}
}
function mouseOutOfDataItem(mouseEvent) {
map.getDiv().removeAttribute('title');
}

I think you will have to do it yourself.In a page i have implemented i attached a mouse move event to the page so i can record the mouse position.Then when a polygon mouseover event occurs i display a custom div near the mouse position
Hope it helps

This code works for me:
googleShape - is your polygon or circle or rectangle.
titleText - message you need to post on hover of shapes.
google.maps.event.addListener(googleshape, 'mouseover', function() {
this.map.getDiv().setAttribute('title', "`titleText`");
});
google.maps.event.addListener(googleshape, 'mouseout', function() {
this.map.getDiv().removeAttribute('title');
});

You could try the following
//Add a listener for the click event
google.maps.event.addListener('click', showArrays);
infoWindow = new google.maps.InfoWindow;
then when the click event happens call the following function
function showArrays(event) {
var contentString = 'Content here';
// Replace the info window's content and position.
infoWindow.setContent(contentString);
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
}

A nice solution will be to use Google's built in InfoWindow as a tooltip/popup container. Then listen to mouseover and mouseout to show/hide the tooltip.
Notice that by using InfoWindow you can also put HTML markup as the content of the tooltip and not only plain text.
const mapHandler: google.maps.Map = ... // your code here
const polygon: google.maps.Polygon = ... // your colde here
const popUp = new google.maps.InfoWindow({
content: "<span style='color:red'> SOME MESSAGE </span>" // red message
});
polygon.addListener("mouseover", (event) => {
popUp.setPosition(event.latLng);
popUp.open(mapHandler);
});
polygon.addListener("mouseout", (event) => {
popUp.close()
});

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 Maps v3 href link for polyline

First post so bear with me.
I have a google map that displays polylines.
I create the polylines using the following method:
function createPoly(number,path, color, name) {
var g = google.maps;
var poly = new g.Polyline({
path: path,
strokeColor: color,
strokeOpacity: 1,
strokeWeight: 3
});
var label = new Label({ map: map }, poly);
// Add mouse events to poly
g.event.addListener(poly, "mouseover", function() {
label.add_(name);
poly.setOptions({strokeWeight: 6.0,strokeColor: "#0000FF",});
});
g.event.addListener(poly, "mouseout", function() {
label.remove_();
poly.setOptions({strokeWeight: 3.0,strokeColor: "#FF0000",});
});
g.event.addListener(poly, "click", function() {
$('.LORDesc').empty();
$('.LORDesc').append(name);
});
poly.setMap(map);
return poly;
}
The above code works, and every time I want to create a poly on my map I use:
var MyPoly = createPoly(0,SC001, "#FF0000", "SC001 <BR>Poly 1");
which works fine also.
My problem is I want to create Links to the side of my map and allow the user to hover over the link and have the poly change weight and color. Like it does when the user hovers over the poly on the map. Basically to identify the poly as they hover over the links.
I hope this makes sense.
Any help would be appreciated. I have tried various techniques on my own but have failed.
Regards,
Jonny
You can listen for mouseover and mouseout events on link and trigger event for polyline object. For example:
var linkId = document.getElementById('polylink');
google.maps.event.addDomListener(linkId, 'mouseover', function(event) {
google.maps.event.trigger(polyLine, 'mouseover', {
stop: null,
latLng: new google.maps.LatLng(48, 13.5),
edge: 0,
path: 0,
vertex: 0
});
});
google.maps.event.addDomListener(linkId, 'mouseout', function(event) {
google.maps.event.trigger(polyLine, 'mouseout', {
stop: null,
latLng: new google.maps.LatLng(48, 13.5),
edge: 0,
path: 0,
vertex: 0
});
});
See complete example at jsbin.
If you don't provide the 3rd parameter for trigger method you will get the message error in console:
Uncaught TypeError: Cannot read property 'vertex' of undefined
mouseover and mouseout polyline event handler provides google.maps.PolyMouseEvent object. I made it like it was suggested for google.maps.MouseEvent object in question Simulate a click in a Google Map.
I just add {} to third parameter of trigger function and this error don't show again.
Example: google.maps.event.trigger(yourPolygon, 'mouseover', {});

Google Maps API v3, jQuery , Tabs, map not showing on hidden div

This question had already been addressed multiple times, but all the solutions don't fix the problem in my case. It's as many already wrote: tabs used to display different parts of content, when loading the map in tab1 i.e. the one that opens on pageload, it displays fine.
When loading the map inside one of the other tabs, that need to be clicked, to be called, it doesn't draw the map as it is supposed to.
I already tried adding the google.maps.event.trigger(map, 'resize'); when the tab is clicked, but it doesn't do anything.
Here is my code, i am going crazy over this, have spent 10+ hours trying all suggestions !
This is the part that handles the tabs being clicked :
$(document).ready(function() {
//Default Action
$(".tab_content").hide(); //Hide all content
$("ul.tabs li:first").addClass("active").show(); //Activate first tab
$(".tab_content:first").show(); //Show first tab content
//On Click Event
$("ul.tabs li").click(function() {
$("ul.tabs li").removeClass("active"); //Remove any "active" class
$(this).addClass("active"); //Add "active" class to selected tab
$(".tab_content").hide(); //Hide all tab content
var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
if (activeTab=='#tab4') {
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
}
$(activeTab).fadeIn(
); //Fade in the active content
return false;
});
});
When using an alert to see if the code is called on tab4 it works.
Here is the googlemaps code
function initialize() {
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(<? echo $lattie; ?>, <? echo $langie; ?>),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var marker = new google.maps.Marker({
position: map.getCenter(),
map: map,
title: 'Click to zoom'
});
google.maps.event.addListener(map, 'center_changed', function() {
// 3 seconds after the center of the map has changed, pan back to the
// marker.
window.setTimeout(function() {
map.panTo(marker.getPosition());
}, 3000);
});
google.maps.event.addListener(marker, 'click', function() {
map.setZoom(8);
map.setCenter(marker.getPosition());
});
}
google.maps.event.addDomListener(window, 'load', initialize);
I am calling intitalize() in the body tag, changing this to the place where i am calling the resize, doesn't solve it either.
Finally the bit that shows the div with the map-canvas :
<li id="four">
<div id="map-canvas" style="width:575px; height:500px;float:left;border:1px solid #d74c15;"></div>
</li>
As i said, i am going crazy over this, i just can't get it to work, any help would be much appreciated !
This is a little bit tricky in one of my projects some time ago I got stuck on the same thing while trying to hide and show the map via a link.For me the resize event handler was all I needed but make sure to add the following code after it:
UPDATE here is the complete function used.
function showMap(){
if(animating == false)
{
animating = true;
if(mapVisible == false)
{
$("#map-wrapper").animate({
height:352
},500,function(){
$("#create-event-map").fadeIn("fast");
google.maps.event.trigger(eventsMap, 'resize');
var center = new google.maps.LatLng(38,24);
eventsMap.setCenter(center);
$("#show-map-wrapper a").html("Hide map");
mapVisible = true;
animating = false;
})
}
else
{
$("#create-event-map").fadeOut("fast",function(){
$("#map-wrapper").animate({
height:0
},500);
$("#show-map-wrapper a").html("Show map");
mapVisible = false;
animating = false;
})
}
}
return false;
}
working example here: http://goo.gl/2syX8
it's in the create.event.init.js file if you want to check it out
UPDATE
In your case if you want to refresh the map you should change this code:
if (activeTab=='#tab4') {
google.maps.event.trigger(map, 'resize');
map.setZoom( map.getZoom() );
}
$(activeTab).fadeIn(
); //Fade in the active content
With this:
if(activeTab == 'YOUR TAB ID')
{
$(activeTab).fadeIn(function(){
google.maps.event.trigger(map, 'resize');
var center = new google.maps.LatLng(<LAT>,<LNG>);
map.setCenter(center);
map.setZoom( map.getZoom() );
})
}
Store the last used lat and lng and replace them in this piece of code.

Is there a way to create a context menu for a gmap marker in primefaces?

Is there a way to create a context menu for a gmap marker in primefaces?
I know there is a gmapInfoWindow object, but that requires an onclick event and doesn't have the look and feel of a context menu.
I would prefer to display a context menu that appears during the onmouseover event for a gmap marker.
Please let me know if this is possible or not? Any sample code would be greatly appreciated also.
Thanks
Joe
Looking at the source code of gmap.js
I just removed the click handlers and added a listener for mouseover:
Here is the javascript code:
jQuery(window).load(function() {
var _self = gmap;
for(var i=0; i < gmap.cfg.markers.length; i++) {
var marker = gmap.cfg.markers[i];
// remove all marker listener
google.maps.event.clearInstanceListeners(marker);
// add mouse over listener
google.maps.event.addListener(marker, 'mouseover', function(event) {
_self.fireOverlaySelectEvent(event, this);
});
// Since we cleared all listeners adding back drag listener
//marker drag
google.maps.event.addListener(marker, 'dragend', function(event) {
_self.fireMarkerDragEvent(event, this);
});
}
});
Where gmap is the widgetVar attribute of <p:gmap>
<p:gmap widgetVar="gmap" />

google maps mousemove and click listener together

I use a mousemove listener on Google Maps, while I want handle the click event too.
var path = new google.maps.MVCArray;
...
line = new google.maps.Polyline({
map: map,
path: new google.maps.MVCArray([path]),
});
google.maps.event.addListener(map, 'click', function(event) {
path.push(event.latLng);
});
google.maps.event.addListener(map, 'mousemove', function(event) {
if (path.getLength() > 1) path.setAt(path.getLength()-1,event.latLng);
});
I want to follow the mouse with the line, but if the user click on the map, push the polyline's array. But the click event doesn't work...
Any idea?
I think there is a small bug in your code:
On line 5 it should read
path: path,
instead of
path: new google.maps.MVCArray([path]),
Reason:
Your var path is already an MCVArray and the property path of PolylineOptions expects just an MVCArray, but you supply it with an MVCArray inside a normal array inside an MVCArray.
This prevents the code following from pushing the coordinates to the correct array.