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>
Related
I have checked multiple similar posts on SO but haven't yet found the answer. I have a Google map app that when you Zoom in the marker changes from an icon to a circle. It works great. The Marker icon is replaced by the circle object. But, I also want it to work in reverse: As you zoom out, I want to remove the circle and replace it with the icon. I can get the icon to "reappear" but I can't figure out a way to get a reference to the circle object that is bound to the Marker so I can remove it.
This is NOT the complete code that I am using but to satisfy the request for something MINIMAL rather than complete, this would create the issue:
var marker;
createamarker();
removeCircle();
function createamarker(){
marker = new google.maps.Marker({
position: location,
map: map,
icon: icon,
// Add some custom properties
obscure:obscure,
originalpin: icon
});
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: 1000, // in metres
fillColor: '#AA0000'
});
// Bind it to the marker
circle.bindTo('center', marker, 'position');
}
I also have a second function that is SUPPOSED to remove the circle:
function removeCircle(){
// remove whatever is there
marker.setMap(null);
var icon = {
url: marker.originalpin,
scaledSize: new google.maps.Size(22,32)
}
// reset the marker icon
marker.icon = icon;
//sets the marker back
marker.setMap(map);
// NOW REMOVE the circle:
// So at this point I am stuck. I have bound a circle to
// the marker but in order to REMOVE the circle I need a
// reference to it. Other SO postings suggest acting on the
// circle object directly like so:
circle.setMap(null);
// but the "circle" doesn't exist here. It was bound to the marker in another function. I need a reference to the circle that was bound to the marker so I can act on it.
}
To do what you are looking to do, one option would be to set the circle as a property of the marker:
marker.circle = circle;
Then you can hide the circle like this:
marker.circle.setMap(null);
Note that this won't work if the circle is global, it needs to be local to the createamarker function.
proof of concept fiddle
code snippet:
var map;
function initialize() {
map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = createamarker(map.getCenter());
removeCircle(marker);
var marker2 = createamarker(new google.maps.LatLng(37.45, -122.14));
google.maps.event.addDomListener(document.getElementById('toggle'), 'click', function() {
marker.circle.setMap(marker.circle.getMap() == null ? map : null);
marker2.circle.setMap(marker2.circle.getMap() == null ? map : null);
});
}
google.maps.event.addDomListener(window, "load", initialize);
function createamarker(location) {
var icon = "http://maps.google.com/mapfiles/ms/micons/blue.png";
marker = new google.maps.Marker({
position: location,
map: map,
icon: icon,
// Add some custom properties
// obscure: obscure,
originalpin: icon
});
// Add circle overlay and bind to marker
var circle = new google.maps.Circle({
map: map,
radius: 1000, // in metres
fillColor: '#AA0000'
});
// Bind it to the marker
circle.bindTo('center', marker, 'position');
marker.circle = circle;
return marker;
}
function removeCircle(marker) {
// remove whatever is there
marker.setMap(null);
var icon = {
url: marker.originalpin,
scaledSize: new google.maps.Size(22, 32)
}
// reset the marker icon
marker.icon = icon;
//sets the marker back
marker.setMap(map);
// NOW REMOVE the circle:
// So at this point I am stuck. I have bound a circle to
// the marker but in order to REMOVE the circle I need a
// reference to it. Other SO postings suggest acting on the
// circle object directly like so:
marker.circle.setMap(null);
// but the "circle" doesn't exist here. It was bound to the marker in another function. I need a reference to the circle that was bound to the marker so I can act on it.
}
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<input type="button" value="toggle circle" id="toggle" />
<div id="map_canvas"></div>
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!
Trying to 'mark' an area with color and place a number in that area:
I have illustrated it here:
the numbers are static and don't change.
The area mark is suppose to change colors. and the area marking suppose to surround the area using the streets/roads around it(not just plain circle drawing)
I will try to explain myself better, Suppose those numbers are areas that I need to visit..
initially they are colored with red. If I visit one area .. then when i finish the visit the marking is turning to blue color.
Hope I make sense. I don't have any code for that .. I tried to search for it but with no luck
I'll try to simplify it, I can manage to drop the colors to not change and make it static also
for that I need to draw on the map some 'areas' but from the streets/roads surrounding the area
only. by that I mean not to draw a line between two points.
Here is one solution. Another might be an image overlay but I believe the solution below is more flexible.
You will need: http://google-maps-utility-library-v3.googlecode.com/svn/trunk/maplabel/src/maplabel-compiled.js
In the above javascript file once you have it, you will also need to change mapPane.appendChild(a) to floatPane.appendChild(a) this is to get the text on top of the polygon. As you will see in the following JSFIDDLE the text is underneath the polygon.
SOLUTION: http://jsfiddle.net/yN29Z/
The above javascript is map_label.js in the code below.
My polygon is not the best but you get the idea.
UPDATE: Added color change on clicking the polygon in to code below.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Polygon Arrays</title>
<style>
html, body, #map-canvas
{
height: 100%;
margin: 0px;
padding: 0px;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script src="scripts/map_label.js" type="text/javascript"></script>
<script>
var map;
var infoWindow;
var mypolygon;
function initialize() {
var mapOptions = {
zoom: 18,
center: new google.maps.LatLng(50.71392, -1.983551),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
// Define the LatLng coordinates for the polygon.
var triangleCoords = [
new google.maps.LatLng(50.71433, -1.98392),
new google.maps.LatLng(50.71393, -1.98239),
new google.maps.LatLng(50.71388, -1.98226),
new google.maps.LatLng(50.71377, -1.98246),
new google.maps.LatLng(50.71332, -1.98296),
new google.maps.LatLng(50.71334, -1.98324),
new google.maps.LatLng(50.71374, -1.9845),
new google.maps.LatLng(50.71436, -1.98389)];
// Construct the polygon.
mypolygon = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35
});
mypolygon.setMap(map);
//Define position of label
var myLatlng = new google.maps.LatLng(50.71392, -1.983551);
var mapLabel = new MapLabel({
text: '1',
position: myLatlng,
map: map,
fontSize: 20,
align: 'left'
});
mapLabel.set('position', myLatlng);
// Add a listener for the click event. You can expand this to change the color of the polygon
google.maps.event.addListener(mypolygon, 'click', showArrays);
infoWindow = new google.maps.InfoWindow();
}
/** #this {google.maps.Polygon} */
function showArrays(event) {
//Change the color here
mypolygon.setOptions({ fillColor: '#0000ff' });
// Since this polygon has only one path, we can call getPath()
// to return the MVCArray of LatLngs.
var vertices = this.getPath();
var contentString = '<b>My polygon</b><br>' +
'Clicked location: <br>' + event.latLng.lat() + ',' + event.latLng.lng() +
'<br>';
// Iterate over the vertices.
for (var i = 0; i < vertices.getLength(); i++) {
var xy = vertices.getAt(i);
contentString += '<br>' + 'Coordinate ' + i + ':<br>' + xy.lat() + ',' + xy.lng();
}
// Replace the info window's content and position.
infoWindow.setContent(contentString);
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas">
</div>
</body>
</html>
My sources were as follows.
For the polygon:
https :// developers.google.com/maps/documentation/javascript/examples/polygon-arrays
For the label
Placing a MapLabel on top of a Polygon in Google Maps V3
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');
});
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()
});