infowindow closing and var (google maps api v3) - javascript

I have problem with closing infowindow when new is open in google maps api v.3.
var infowindow;
function addMarker(id, location) {
contentString = 'some content';
var marker = new google.maps.Marker({
position: location,
map: map
});
var infowindow = new google.maps.InfoWindow({
content: contentString,
maxWidth: 200
});
google.maps.event.addListener(marker, 'click', function() {
if (infowindow) infowindow.close();
infowindow.open(map, marker);
});
markersArray[id] = marker;
}
The problem is that in above code the old infowindow is not close when new one is clicked unless I delete var from the line var infowindow = new google.maps.InfoWindow({
But then all the infowindows have the same content...
Any help?
Thanks.

Use a global variable to contain a pointer to the last opened marker, and when opening a new one, close the previous one. Like this
//var infowindow; // <-- removed; should not be global
var openedInfoWindow = null; // <-- added this here; make sure it's global
function addMarker(id, location) {
contentString = 'some content';
var marker = new google.maps.Marker({
position: location,
map: map
});
var infowindow = new google.maps.InfoWindow({
content: contentString,
maxWidth: 200
});
google.maps.event.addListener(marker, 'click', function() {
if (openedInfoWindow != null) openedInfoWindow.close(); // <-- changed this
infowindow.open(map, marker);
// added next 4 lines
openedInfoWindow = infowindow;
google.maps.event.addListener(infowindow, 'closeclick', function() {
openedInfoWindow = null;
});
});
markersArray[id] = marker;
}

Related

Google maps add infowindow to marker

Hi i building a store locator and i found this code online its workind fine
function generateMarkers(points) {
//var iconBase = "https://media.nisbets.com/static/content/banners/";
for (var i = 0; i < points.length; i++) {
var place = points[i];
var myLatLng = new google.maps.LatLng(place[0], place[1]);
var marker = new google.maps.Marker({
position: myLatLng,
name: place[2],
clickable: true,
map: map,
icon: /*iconBase + */"nisbets_storelocator_marker.png",
url: place[3]
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url;
});
setMapBounds(marker.getPosition());
markers.push(marker);
}
}
But instead of a link who redirect to a page i want a popup window with the place info inside the popup.
Please i need your help how to add the infoWindow to my code.
Thanks
Define a new InfoWindow
var infowindow = new google.maps.InfoWindow({
content: contentString
});
Add an event listener to each marker to open the InfoWindow.
marker.addListener('click', function() {
infowindow.open(map, marker);
});
Reference: https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

Google maps marker - infowindow

I need to in InfoWindow show match.offer.text.
Each marker has a different match.offer.text.
This is my code:
var markerImageURL, lat, lng;
if (myoffer) {
markerImageURL = 'assets/img/markers/marker_my.png';
lat = match.lat;
lng = match.lng;
} else {
markerImageURL = 'assets/img/markers/marker_' + match.strength + '.png';
lat = match.offer.lat;
lng = match.offer.lng;
}
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: window.googleMap,
icon: {
size: new google.maps.Size(54,56),
url: markerImageURL
},
draggable: false,
visible: true
});
var infowindow = new google.maps.InfoWindow({
content: match.offer.text,
maxWidth: 300
});
window.googleMapMarkers.push(marker);
if(!myoffer) {
window.MVC.Events.GoogleMap.prototype.showInfoWindow(marker, infowindow, match);
}
Event triggered after clicking on the marker:
marker.addListener('click', function() {
infowindow.open(window.googleMap, marker);
}
Please, help me.
The content is applied to the marker on Open therefore your code will apply the content in last item in your loop to all markers, in your openWindow function add the content to a single infoWindow object i.e.
Also the Maps API has its own event wrapper for the click event
function initMarkers(){
//create markers
var marker = new google.maps.Marker();
google.maps.event.addListener(marker, 'click', openInfoWindow(marker, i));
}
var infowindow = new google.maps.InfoWindow();
function openInfoWindow(marker,index){
return function(e) {
//Close other info window if one is open
if (infowindow) {
infowindow.close();
}
var content = marker.offer.text;
infowindow.setContent(content);
setTimeout(function() {
infowindow.open(map, marker);
}, 200);
}
}

Fix Size in Google Map

I am Integrating google map.When I hover on marker it shows me location on marker.But my problem is when I hover on marker map moves. I want when I hover on marker position should be fixed.Here is My code:
function CreateMarker(Obj){
var $j=jQuery.noConflict();
var pos;
var allMarkers = [];
pos = new google.maps.LatLng(Obj['latitude'], Obj['longitude']);
var marker = new google.maps.Marker({
position: pos,
map: map,
zoom:14,
icon: gicons["blue"]
});
latlngbounds.extend(pos);
var str = '<div class="google_popup"><span style="color:#00aeef; font-weight:bold; font-size: 14px; ">'+Obj['name']+'</span></b><div>'+Obj['address']+'</div><div>'+Obj['city']+', '+Obj['state']+' '+Obj['zip']+'</div><div> '+Obj['email']+'</div><div>'+Obj['phone']+'</div></div>';
google.maps.event.addListener(marker, "click", function() {
map.panTo(pos);
map.setZoom(14);
info.setContent(str);
info.open(map, marker);
//openTrInfo($("#" + Obj['elemID'])[0], false);
var emid=Obj['elemID'];
merchant_deals(emid);
});
google.maps.event.addListener(marker, "mouseover", function () {
marker.setIcon(gicons["grey"]);
// map.panTo(pos);
info.setContent(str);
info.open(map, marker);
var emid=Obj['elemID'];
});
google.maps.event.addListener(marker, "mouseout", function () {
marker.setIcon(gicons["blue"]);
info.close(map, marker);
});
gmarkers.push(marker);
return marker;
}
When you create your infowindow, set the disableAutoPan property to false. By default this is true, so the map automatically pans when the infowindow opens, when you do info.open(map, marker);
https://developers.google.com/maps/documentation/javascript/reference#InfoWindowOptions
var info = new google.maps.InfoWindow();
var info = new google.maps.InfoWindow({
disableAutoPan: true
});

How to set a popup on markers with Google Maps API?

I have this code where I display and set all my markers. How can I add a popup with some information on markers with this code? I add "i" variable on text, but it sets on all markers popup with "test - 723", where 723 is last value of the "i" variable. What is wrong?
for (var i = 0; i < arraylng.length-1; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(arraylng[i], arraylat[i])
});
var infowindow = new google.maps.InfoWindow({
content: " "
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent('test: ' + i + '');
infowindow.open(map, this);
});
markers.push(marker);
}
First, change the loop condition to i < arraylng.length. Right now it is not capturing the last element of your array.
JavaScript variables works with function scope so you need to call a function for each marker listener to create the correct variable references. You can use an anonymous function, as seen here, or define a function for creating the click listener:
Multiple infoWindows:
function makeInfoWindowEvent(map, infowindow, marker) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map, marker);
});
}
Most likely you will not want more than one InfoWindow open at the same time, because it is annoying to have to click on the close x. Then you will only want one InfoWindow object, and set the content as the marker is clicked:
Single InfoWindow:
...
var infowindow = new google.maps.InfoWindow();
for (var i = 0; i < arraylng.length-1; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(arraylng[i], arraylat[i]),
map: map
});
makeInfoWindowEvent(map, infowindow, "test" + i, marker);
markers.push(marker);
}
}
function makeInfoWindowEvent(map, infowindow, contentString, marker) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map, marker);
});
}
this is the area where you can add your content of popup
var infowindow = new google.maps.InfoWindow({
content: "Add your popup content here"
});
and this is for showing your popup
marker.addListener('click', function() {
infowindow.open(map, marker);
});
Below code shows how its work and use.
features.forEach(function(feature) {
var infowindow = new google.maps.InfoWindow({
content: "Add your popup content here"
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat,long),
icon: "image.png",
/*icon: icons[feature.type].icon,*/
title: "Title for marker",
map: map
});
marker.addListener('click', function() {
infowindow.open(map, marker);
});
});
var infowindow = new google.maps.InfoWindow();
var marker = new google.maps.Marker({
position: myLatLng,
....
content: point[4]
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(this.content);
infowindow.open(map, this);
});
Code inside loop. This worked for me perfectly.
That's because the variable i is not used in the loop but when the marker is clicked -- and then i is equal to the last index + 1... The addListener is asynchronous, not synchronous.
Remove infowindow.setContent('test: ' + i + ''); and replace content: " " with content: 'test: ' + i. This should fix your problem.

Multiple markers with infowindow

We using multiple marker with infowindow. Everything is working fine.
The issue is when we click marker, the infowindow opens but it doesn't close when click other markers. it stay opened. so can give solutions for this issue.
The code is in http://goo.gl/s0WZx
var berlin = new google.maps.LatLng(52.520816, 13.410186);
var neighborhoods = [
new google.maps.LatLng(52.511467, 13.447179),
new google.maps.LatLng(52.549061, 13.422975),
new google.maps.LatLng(52.497622, 13.396110),
new google.maps.LatLng(52.517683, 13.394393)
];
var markers = [];
var iterator = 0;
var map;
function initialize() {
var mapOptions = {
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
center: berlin
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
function drop() {
for (var i = 0; i < neighborhoods.length; i++) {
setTimeout(function() {
addMarker();
}, i * 200);
}
}
function addMarker() {
var marker = new google.maps.Marker({
position: neighborhoods[iterator],
map: map,
draggable: false
});
markers.push(marker);
var contentString = $("#pop"+iterator).html();
var infowindow = new google.maps.InfoWindow({
content: contentString,
maxWidth: 300,
maxHeight: 500
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
iterator++;
}
You are creating as much infowindows, as there are markers. In your case,I think one infowindow is enough. So for working with only one infowindow, you could implement this on global scope:
//Using lazy initialization.
//InfoWindow will be created only after the first call
var getInfoWindow = (function(){
var _instance = null;
return function(){
if(_instance == null){
_instance = new google.maps.InfoWindow({
maxWidth: 300,
maxHeight: 500
});
}
return _instance;
};
})();
Also you need to store the contentString for every marker which must be shown on click event. So the final modification of addMarker method will be something like this:
function addMarker() {
var marker = new google.maps.Marker({
position: neighborhoods[iterator],
map: map,
draggable: false
});
markers.push(marker);
//Storing content html
marker.contentString = $("#pop"+iterator).html();
google.maps.event.addListener(marker, 'click', function() {
//Setting content of InfoWindow
getInfoWindow().setContent( marker.contentString );
//Opening
getInfoWindow().open(map,marker);
});
iterator++;
}
Put the var infowindow outside of the addMarker function. Like this:
var infowindow = new google.maps.InfoWindow();
Then inside your addMarker function use
infowindow.setContent(contentString);
This way the infowindow is only created once. Clicking on the different markers just moves the window and sets the content.

Categories

Resources