on-hover marker icons get change on google map - javascript

I have seen their are several such type of question already asked according to those answers one more new issue is raised,
I have shown multiple markers on map, now I need if I hover to specific marker then icon get changed and when I remove my cursor from the marker then set previous icon, below is my code
for (i = 0; i < Object.size(locations); i++) {
marker = new google.maps.Marker({
position : new google.maps.LatLng(locations[i][1], locations[i][2]),
icon : site_url+'/assets/front/images/'+locations[i][4],
map : map,
url : site_url+'/detail/'+locations[i][3],
draggable: false,
hovericon: site_url+'/assets/front/images/'+hovericon,
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url;
});
google.maps.event.addListener(marker, "mouseover", function() {
marker.setIcon(this.hovericon);
});
google.maps.event.addListener(marker, "mouseout", function() {
marker.setIcon(this.icon);
});
markers.push(marker);
function AutoCenter() {
var bounds = new google.maps.LatLngBounds();
$.each(markers, function (index, marker) {
bounds.extend(marker.position);
});
map.fitBounds(bounds);
}
AutoCenter();
}
Above code is showing multiple markers that is good but when I hover to specific marker then it changing always last marker icon, but I need that icon should be changed only which I hover not the last one.
If I hover to any marker always last icon get changed see in attached image If I hover to first, second last icon is changing.
What I am doing wrong, any help ?

this happend because when the for ends you have the last marker to change attached to the listener.
You can use this insetead of marker inside the addListeners to get the intended one.
for (i = 0; i < Object.size(locations); i++) {
marker = new google.maps.Marker({
position : new google.maps.LatLng(locations[i][1], locations[i][2]),
icon : site_url+'/assets/front/images/'+locations[i][4],
map : map,
url : site_url+'/detail/'+locations[i][3],
draggable: false,
originalicon: site_url+'/assets/front/images/'+locations[i][4],
hovericon: site_url+'/assets/front/images/'+hovericon
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url;
});
google.maps.event.addListener(marker, "mouseover", function() {
this.setIcon(this.hovericon);
});
google.maps.event.addListener(marker, "mouseout", function() {
//you have to retreive the original icon cause with the mouse hover you change the marker icon attribute
this.setIcon(this.originalicon);
});
markers.push(marker);
function AutoCenter() {
var bounds = new google.maps.LatLngBounds();
$.each(markers, function (index, marker) {
bounds.extend(marker.position);
});
map.fitBounds(bounds);
}
AutoCenter();
}
This way you refer the element that you are interacting with(mouseover and mouseout) and not the last one in the for loop.
I didn't tested the code so I'm not sure is working.
look at this fiddle for working sample
hope this helps

Related

How to remove an empty div created by Google Maps InfoWindow (Close info window on map click also close older info window's when opening new one)

For some reason, Google is rendering an empty div in Google Maps infoWindow card. I'm following the normal practice in which I'm initiating Google Maps via initMap() and to add infoWindow I'm using:
if (props.content) {
var infoWindow = new google.maps.InfoWindow({
content: props.content
});
marker.addListener('click', function() {
infoWindow.open(map, marker);
});
}
How do I remove this div? or place my content in the div?
For deleting a div in javascript you can simply use remove() function via dot notation.
Just like this:
document.getElementById("element").remove();
But since you don't have a special id or class in your desired DOM item that couldn't be a solution. So you have to get the parent node and remove its children. All you have to do is to use removeChild(/*element.lastChild*/). So it will be something like this:
const element = document.querySelector('.gm-style-iw-d');
element.removeChild(element.lastChild);
UPDATE
If you want to disable the context menu in your other div to make them look like the same, there are two ways to do that.
First, you can do this by manipulating HTML itself like this:
<div oncontextmenu="return false;"></div>
Or by manipulating the DOM after the initial load:
HTML
<div id='elementId'></div>
Javascript
document.getElementById('elementId').addEventListener('contextmenu', function() {
return false;
});
UPDATE 2
This is not the best approach to achieve what you wanted but you can do this in order to close the dialog whenever the user clicks anywhere in the map except marker and dialog itself. According to the google map document you can use .close() method to close InfoWindow just like this:
function addMarker(props) {
var marker = new google.maps.Marker({
position: props.coords,
map: map,
});
// Check for custom icon
if (props.iconImage) {
// Set icon image
marker.setIcon(props.iconImage);
}
// Check content
if (props.content) {
var infoWindow = new google.maps.InfoWindow({
content: props.content
});
marker.addListener('click', function() {
infoWindow.open(map, marker);
setTimeout(() => {
var x = document.getElementsByClassName('gm-ui-hover-effect')[0].remove();
}, 1);
});
}
google.maps.event.addListener(map, "click", function(event) {
infoWindow.close();
});
}
UPDATE 3
To achieve closing old infoWindow you should create a unique infoWindow and update its content to having just one that gets closed/opened on each marker.click. So in this way, it will be working like a charm.
var infoWindow = null;
// Add marker function
function addMarker(props) {
var marker = new google.maps.Marker({
position: props.coords,
map: map,
// icon: './img/marker1.png'
});
// Check for custom icon
if (props.iconImage) {
// Set icon image
marker.setIcon(props.iconImage);
}
// Check content
if (props.content) {
google.maps.event.addListener(marker, 'click', function() {
if (infoWindow) {
infoWindow.close();
}
infoWindow = new google.maps.InfoWindow({
content: props.content
});
infoWindow.open(map, marker);
setTimeout(() => {
var x = document.getElementsByClassName('gm-ui-hover-effect')[0].remove();
}, 10);
});
}
google.maps.event.addListener(map, "click", function(event) {
infoWindow.close();
});
}
Here is the link to the working demo: JSFiddle

Adding an Infobox to GoogleMaps?

I'm trying to add an infobox to a number of pins on a Google map.
I've got the infobox to work previous - JSfiddle.
But i've now altered the code to include a dropdown menu. I've tried to add the infoboxes again but it doesn't work and i don't understand why it doesn't. JSfiddle.
I've copied and pasted the code for the infoboxes, checked that the variable names still match up, and that the code is in the same order.
The following code is where the marker is added to the map and i'm trying to add an infobox. The function addMarker is sat within my initialisation function. And var infowindow is sat beneath the initialisation function.
function addMarker(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: feature.icon,
map: map,
size: 20,
title: name,
draggable: false,
//animation: google.maps.Animation.DROP
});
google.maps.event.addListener(marker, 'click', (function (marker) {return function () {
var Infocontent = feature.desc;
infowindow.setContent(Infocontent);
infowindow.open(map, marker); //'mouseover'
}
})(marker));
}
var infowindow = new google.maps.InfoWindow();
May someone please tell me where i've gone wrong?
I just updated your code by creating a new fiddle and now it is showing infowindow. I just moved your infowindow code within listener definintion.
Please check
google.maps.event.addListener(marker, 'click', (function (marker) {return function () {
console.log('called');
var Infocontent = feature.desc;
infowindow = new google.maps.InfoWindow();
infowindow.setContent(Infocontent);
infowindow.open(map, marker); //'mouseover'
}
})(marker));
JSFiddle

Open info window on load Google Map

I'm wondering if it's possible to open one of the infoWindow objects that are attached to each marker in the code below on page load and not just by clicking on them? As it is now, the user has to click on one of the markers to open an info window.
I tested to create a "stand alone" info window object and that opened fine onload, but it didn't close when I clicked on some of the other markers, because the onClick function was attached to the markers that only could close the info windows attached to that object. Correct med if I'm wrong?
Would this be possible and can I "call" an object by the number or what options do I have? Tips are preciated!
Or if there is possible, that I have tried to open an separate info window onload and be able to close that if I open one of the other info windows!?
var map = null;
var infowindow = new google.maps.InfoWindow();
var iconBase = 'images/mapNumbers/number';
//var zoomLevel = 11;
//var mapPositionLat = 55.678939;
//var mapPositionLng = 12.568359;
function initialize() {
var markerPos = new google.maps.LatLng(55.674196574861895, 12.583808898925781);
var myOptions = {
zoom: 11,
//center: new google.maps.LatLng(55.678939, 12.568359),
center: markerPos,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function () {
infowindow.close();
});
google.maps.event.addListener(map, 'zoom_changed', function () {
infowindow.close();
});
google.maps.event.addDomListener(window, 'resize', function() {
map.setCenter(markerPos);
map.setZoom(zoomLevel);
//var center = map.getCenter();
});
// Add markers to the map
var point;
point = new google.maps.LatLng(55.667093,12.581255); createMarker(point, "<div class='infoWindow'>1</div>");
point = new google.maps.LatLng(55.660794,12.58972); createMarker(point, "<div class='infoWindow'>2</div>");
point = new google.maps.LatLng(55.660491,12.587087); createMarker(point, "<div class='infoWindow'>3</div>");
}
// Create markers
function createMarker(latlng, html, name, number) {
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: name,
icon: iconBase + number + '.png'
});
google.maps.event.addListener(marker, 'click', function () {
infowindow.setContent(html);
infowindow.open(map, marker);
//map.setCenter(marker.getPosition());
map.setCenter(55.678939, 12.568359);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
In order to display InfoWindow when the map loads, make the call to
infowindow.open(map, marker);
outside of the marker listener.
Below is demonstrated createMarker function, where parameter displayInfoWindow defines whether to display InfoWindow when the map loads:
// Create marker
function createMarker(map,markerPos, markerTitle,infoWindowContent,displayInfoWindow) {
var marker = new google.maps.Marker({
position: markerPos,
map: map,
title: markerTitle,
});
var infowindow = new google.maps.InfoWindow({
content: infoWindowContent
});
if(displayInfoWindow) {
infowindow.open(map, marker);
}
google.maps.event.addListener(marker, 'click', function () {
infowindow.open(map, marker);
});
}
Example: http://jsbin.com/lusuquwu/1/
It is possible. One possible solution is to save markers to an array and then trigger click event on of one of them using google.maps.event.trigger(). For example:
...
var zoomLevel = 11; // uncommented due to error message
var markers = [];
function initialize() {
...
point = new google.maps.LatLng(55.660491,12.587087); createMarker(point, "<div class='infoWindow'>3</div>");
google.maps.event.trigger(markers[1], 'click');
}
function createMarker(latlng, html, name, number) {
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: name,
//icon: iconBase + number + '.png'
icon: iconBase
});
// added to collect markers
markers.push(marker);
google.maps.event.addListener(marker, 'click', function () {
console.log('click event listener');
infowindow.setContent(html);
infowindow.open(map, marker);
//map.setCenter(marker.getPosition());
// corrected due to error
map.setCenter(new google.maps.LatLng(55.678939, 12.568359));
});
}
I combined info from this and another site to come up with the below solution as most solutions are for multi-marker maps.
Just a few lines is all you actually need.
// Start with your map
var map = new google.maps.Map(...);
// Now define the info window using HTML. You can insert images etc.
var info = new google.maps.InfoWindow({content: 'YOUR HTML HERE'});
// Now define the marker position on the map
var marker = new google.maps.Marker({map: map, position:{lat: 'YOUR LATITUDE',lng: 'YOUR LONGITUDE'}});
Now we have the variables, just hide the marker, show the info window and set the map zoom and center.
// Set the map zoom
map.setZoom('YOUR ZOOM LEVEL [1 - 20]');
// Set the map center
map.setCenter({lat: 'YOUR LATITUDE',lng: 'YOUR LONGITUDE'});
// Hide the marker that we created
marker.setVisible(false);
// Open the info window with the HTML on the marker position
info.open(map, marker);
For the content, you can create layers and insert images and text as you need. Just make sure you include the full URL to images in your html.
I also recommend adding this to your CSS to hide the close button, which effectively makes this a permanent, info window.
.gm-style-iw + div {display: none;}

trigger google maps marker click

I have a google map set up to find the user's current location, and center the map at that closest marker to their location. The markers, when clicked, open up an infobox (note this is a little different than an infoWindow - http://google-maps-utility-library-v3.googlecode.com/svn/trunk/infobox/docs/examples.html). What I want to do, however, is automatically open up the nearest marker without the user actually clicking. Here's the code to trigger a marker opening:
//loop through all locations to add a marker:
addMarker = function(loc) {
var markerLoc = new google.maps.LatLng(loc.Lat, loc.Long);
var marker = new google.maps.Marker({
map: map,
position: markerLoc
});
google.maps.event.addListener(marker, "mousedown", function() {
var infoOpts = {
content: loc.markerText,
boxStyle: { background: "none transparent", width: "180px"},
pixelOffset: new google.maps.Size(-90, 0),
closeBoxMargin: "5px"
};
var ib = new InfoBox(infoOpts);
ib.open(map, marker);
});
markers.push(marker);
};
So somehow I have to trigger the mouseDown function of the appropriate marker, but it has to be done in a function outside of this one. I will have a reference to the appropriate marker in the array (markers[closestmarker]).
I see that this question has been sitting for quite awhile, but, just in case, this answer may be helpful:
trigger google maps marker click
The code would look like this:
var marker = new google.maps.Marker({});
new google.maps.event.trigger( marker, 'click' );
I found I needed to attach a click event to the marker like so
var marker = new google.maps.Marker({});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
new google.maps.event.trigger( marker, 'click' );

infowindows on pushpins not closing on google maps

Im using google maps api v3. im adding markers by caling this function:
function createMarker(posn, title, html) {
var marker = new google.maps.Marker ({position:posn, title: title, draggable: false});
var infowindow = new google.maps.InfoWindow({content: html});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map,marker);
});
return marker;
}
it works okay, the only problem is when i click a pushpin the window opens, but when i click another pushpin the first pushpins infowindow window does not close both infowindows are visible.
Don't know if you solved this, but the way I did it was:
function createMarker(posn, title, html) {
var marker = new google.maps.Marker ({position:posn, title: title, draggable: false});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map,marker);
});
infowindow = new google.maps.InfoWindow({content: html});
return marker;
}
This works, and the windows close when another pin is clicked, but the "X" close button doesn't work...
You need to keep track of your info windows in an array and close them programmaticaly when a click event fires so using your example
//define a global array
infoWindows = new Array();
//..do your stuff
function createMarker(posn, title, html) {
var marker = new google.maps.Marker ({position:posn, title: title, draggable: false});
var infowindow = new google.maps.InfoWindow({content: html});
//add this infowindow to an array
infoWindows.push(infowindow);
google.maps.event.addListener(marker, "click", function() {
//go through the array and close all open info windows
for (i=0;i<infoWindows.length;i++) {
infoWindows[i].setMap(null);
}
//open current info window
infowindow.open(map,marker);
});
return marker;
}

Categories

Resources