Markers render more than once - google-maps
i've implemented google maps v3, it updates while bounds_changed event but the problem is when the event triggers it renders the markers more than once, any suggestions.
i've added the makers to marker manager.
var map = null;
var myLatLng = null;
var sidebarHtml = "";
var infowindow = infowindow = new google.maps.InfoWindow({});;
var mgr = null;
var currentBounds = null;
var xml = null;
var markers = null;
var markersArray = [];
var customIcons = {
restaurant: {
icon: 'images/icon_01.png'
},
bar: {
icon: 'images/icon_02.png'
}
}
function load() {
var myOptions = ({
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: 'roadmap'
});
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
mgr = new MarkerManager(map, {fitBounds: true});
google.maps.event.addListener(map, 'bounds_changed', downloadUrl);
}
function downloadUrl() {
var request = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest;
var params = "";
var url = "phpmysqlajax_genxml.php";
request.onreadystatechange = function() {
if(request.readyState == 4) {
//alert(request.responseXML);
useTheData(request);
}
};
request.open("GET", url + params, true);
request.send(null);
}
function useTheData(data) {
currentBounds = map.getBounds();
xml = data.responseXML;
markers = xml.documentElement.getElementsByTagName("marker");
mgr.clearMarkers();
if (!currentBounds) currentBounds = new google.maps.LatLngBounds();
sidebarHtml = '<ul class="sidebar">';
for(var i = 0; i < markers.length; i++) {
var name = markers[i].getAttribute("name");
var address = markers[i].getAttribute("address");
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var type = markers[i].getAttribute("type");
var latlng = new google.maps.LatLng(lat, lng);
if (currentBounds.contains(latlng)) {
attachMarker( name, address, latlng, type );
markerSidebarEntry(i);
}
}
sidebarHtml += "</ul>";
document.getElementById("sidebar").innerHTML = sidebarHtml;
};
function attachMarker( name, address, latlng, type ) {
var marker = new google.maps.Marker({
position : latlng,
map: map,
icon : customIcons[type].icon
});
mgr.addMarker(marker, 5);
markersArray.push(marker);
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent( address );
infowindow.open(map, this);
});
// add marker here.
}
function markerSidebarEntry(i) {
var name = markers[i].getAttribute("name");
sidebarHtml += '<li class="' + markers[i].getAttribute("type") + '">' + name + '</li>';
}
function markerClick(i) {
google.maps.event.trigger(markersArray[i], "click");
}
It may just be a copy-paste error, but this code doesn't seem correct (double-declaration of 'infowindow' and duplicated ; at the end):
var infowindow = infowindow = new google.maps.InfoWindow({});;
Also here you don't need the () surrounding the {}
var myOptions = ({
center: new google.maps.LatLng(47.6145, -122.3418),
zoom: 13,
mapTypeId: 'roadmap'
});
Suggest you run your JS code through something like JSLint.
Related
Google maps displaying other side of the planet
I have literally been trying anything and everything, but the map displays somewhere in Antarctica, when the coordinates that I am receiving, are for cities in the US. The following is the code that inits the map: $(window).on('load', function() { // eslint-disable-next-line function initTest() { var fastestCitiesApi = JSON.parse(document.getElementById('fastestCitiesApi').value); console.log(fastestCitiesApi); var tempLatLng = fastestCitiesApi[0]['latLng']; console.log(tempLatLng); var latLng = tempLatLng.split(','); console.log('latLng[0]: ' + latLng[0]); console.log('latLng[1]: ' + latLng[1]); const myLatLng = new google.maps.LatLng({ lat: parseFloat(latLng[0]), lng: parseFloat(latLng[1]) }); const mapOptions = { zoom: 4, center: myLatLng }; const map = new google.maps.Map(document.getElementById('fastest_cities_map'), mapOptions); new google.maps.Marker({ position: myLatLng, map, title: 'somewhere', }); } initTest(); }); As you can see by the following screen shot of the console is showing, that the coordinates are 36, 78 - which when I do a look up on google for Boydton, VA - those are the coordinates that is returned - link to google search for Boydton, VA lat long As you can see by the screen shot, I have zero errors. What am I doing wrong? Thanks in advance
Thanks to #Gray pointing out that the long should be negative, the rest of the array loaded up the markers as expected. Here's the final code: $(window).on('load', function() { // eslint-disable-next-line function initTest() { /* init vars */ const map = new google.maps.Map(document.getElementById('fastest_cities_map')); var fastestCitiesApi = JSON.parse(document.getElementById('fastestCitiesApi').value); // console.log(fastestCitiesApi); var tempLatLng = ''; var latLng = []; var markerOptionsArray = new Array(); var latlngArray = new Array(); var myLat = new Array(); var myLng = new Array(); var markers = []; var nameArray = []; var infowindow = new google.maps.InfoWindow({maxWidth: 350}); /* create arrays for markers */ for(var item in fastestCitiesApi) { tempLatLng = fastestCitiesApi[item]['latLng']; nameArray.push(fastestCitiesApi[item]['city']); // console.log(tempLatLng); latLng = tempLatLng.split(','); myLat.push(latLng[0]); myLng.push(latLng[1]); // console.log('latLng[0]: ' + latLng[0]); // console.log('latLng[1]: ' + latLng[1]); markers.push({ lat: myLat[item], lng: myLng[item], name: nameArray[item], }); } // console.log('markers: ' + JSON.stringify(markers)); var bounds = new google.maps.LatLngBounds(); for (var v=0; v<markers.length; v++) { var secheltLoc = new google.maps.LatLng(markers[v].lat, markers[v].lng); var myOptions = { content: markers[v].name ,boxStyle: { border: "none" ,textAlign: "center" ,fontSize: "12pt" ,width: "120px" } ,disableAutoPan: true ,pixelOffset: new google.maps.Size(-50, 0) ,position: secheltLoc ,closeBoxURL: "" ,isHidden: false ,pane: "mapPane" ,enableEventPropagation: true }; var marker = new google.maps.Marker({ position: new google.maps.LatLng(markers[v].lat, markers[v].lng), map: map, center: new google.maps.LatLng(markers[v].lat, markers[v].lng) }); google.maps.event.addListener(marker, 'click', (function (marker, v) { return function () { infowindow.setContent(markers[v].name); infowindow.open(map, marker); } })(marker, v)); google.maps.event.addListener(map, 'click', function () { infowindow.close(); marker.open = false; }); bounds.extend(new google.maps.LatLng(markers[v].lat, markers[v].lng)); myLatLng = markers[v].lat+','+markers[v].lng; } map.fitBounds(bounds); } // alert(google.maps.Map); initTest(); }); The following is a screen shot of the result of the eight city array:
How can i change the Marker color clicked previously to its original color
I am displaying markers on a Google map . When i click on the marker , i am setting it to different color (blue) like this this.setIcon("http://maps.google.com/mapfiles/ms/icons/blue-dot.png"); This is my full code var map; var global_markers = []; var markers = [[37.09024, -95.712891, 'trialhead0'], [-14.235004, -51.92528, 'trialhead1'], [-38.416097, -63.616672, 'trialhead2']]; var infowindow = new google.maps.InfoWindow({}); function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(40.77627, -73.910965); var myOptions = { zoom: 1, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); addMarker(); } function addMarker() { for (var i = 0; i < markers.length; i++) { // obtain the attribues of each marker var lat = parseFloat(markers[i][0]); var lng = parseFloat(markers[i][1]); var trailhead_name = markers[i][2]; var myLatlng = new google.maps.LatLng(lat, lng); var contentString = "<html><body><div><p><h2>" + trailhead_name + "</h2></p></div></body></html>"; var marker = new google.maps.Marker({ position: myLatlng, map: map, title: "Trailhead name: " + trailhead_name }); marker['infowindow'] = contentString; global_markers[i] = marker; google.maps.event.addListener(global_markers[i], 'click', function() { this.setIcon("http://maps.google.com/mapfiles/ms/icons/blue-dot.png"); infowindow.setContent(this['infowindow']); infowindow.open(map, this); }); } } window.onload = initialize; http://jsfiddle.net/ZLuTg/1008/ My question is that when i click on another Marker , how can i set the prevous marker which is in blue color to its original color
You already have an array of all your markers. Loop over them, resetting their icons. Either do this for all the markers, then set the current one to blue. Or within the loop have an if statement checking if the one being looped over is the currently clicked one (I prefer the first option). google.maps.event.addListener(global_markers[i], 'click', function() { for (var j = 0; j < global_markers.length; j++) { global_markers[j].setIcon("http://maps.google.com/mapfiles/ms/icons/red-dot.png"); } this.setIcon("http://maps.google.com/mapfiles/ms/icons/blue-dot.png"); infowindow.setContent(this['infowindow']); infowindow.open(map, this); });
Your original marker is the default marker. To set it back to that call setIcon(null). google.maps.event.addListener(global_markers[i], 'click', function () { for (var j = 0; j < global_markers.length; j++) { global_markers[j].setIcon(null); } this.setIcon("http://maps.google.com/mapfiles/ms/icons/blue-dot.png"); infowindow.setContent(this['infowindow']); infowindow.open(map, this); }); working fiddle code snippet: var map; var global_markers = []; var markers = [ [37.09024, -95.712891, 'trialhead0'], [-14.235004, -51.92528, 'trialhead1'], [-38.416097, -63.616672, 'trialhead2'] ]; var infowindow = new google.maps.InfoWindow({}); function initialize() { geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(40.77627, -73.910965); var myOptions = { zoom: 1, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP } map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); addMarker(); } function addMarker() { for (var i = 0; i < markers.length; i++) { // obtain the attribues of each marker var lat = parseFloat(markers[i][0]); var lng = parseFloat(markers[i][1]); var trailhead_name = markers[i][2]; var myLatlng = new google.maps.LatLng(lat, lng); var contentString = "<html><body><div><p><h2>" + trailhead_name + "</h2></p></div></body></html>"; var marker = new google.maps.Marker({ position: myLatlng, map: map, title: "Trailhead name: " + trailhead_name }); marker['infowindow'] = contentString; global_markers[i] = marker; google.maps.event.addListener(global_markers[i], 'click', function() { for (var j = 0; j < global_markers.length; j++) { global_markers[j].setIcon(null); } this.setIcon("http://maps.google.com/mapfiles/ms/icons/blue-dot.png"); infowindow.setContent(this['infowindow']); infowindow.open(map, this); }); } } window.onload = initialize; #map_canvas { width: 600px; height: 500px; } <script src="http://maps.google.com/maps/api/js"></script> <div id="map_canvas"></div>
Refresh DownloadUrl on google map v3 and remove old markers
Hi I had a script and a xml file with 10.000 markers in it. I use markercluster to display the markers, but in order to fill the mobile browsers requierement I must begin the map at a certain zoom level (not to display to many markers). So I want to dynamically load the markers when the user change the zoom or viewport. Here is my script. I thing I can't get the lat and lng of the LatLngBounds of the current viewport : var customIcons = { chambrehote: { icon: '/wp-content/themes/codium-extend/images/chambre.png', shadow: '/wp-content/themes/codium-extend/images/icon-shadow.png' } }; function initialize() { var cluster = []; var map = new google.maps.Map(document.getElementById("map_canvas"), { center: new google.maps.LatLng('45.7676067','4.8351733'), scrollwheel: false, zoom: 12, mapTypeId: 'roadmap' }); var mcOptions = {gridSize: 100, styles: [ { textColor: 'black', height: 80, url: "/wp-content/themes/codium-extend/images/cluster-m1.png", width: 90 }, { textColor: 'black', height: 80, url: "/wp-content/themes/codium-extend/images/cluster-m2.png", width: 90 }, { textColor: 'black', height: 80, url: "/wp-content/themes/codium-extend/images/cluster-m3.png", width: 90 } ]}; var infoWindow = new google.maps.InfoWindow; var bounds = new google.maps.LatLngBounds(); var swPoint = bounds.getSouthWest(); var nePoint = bounds.getNorthEast(); var swLat = swPoint.lat(); var swLng = swPoint.lng(); var neLat = nePoint.lat(); var neLng = nePoint.lng(); downloadUrl("/wp-content/themes/codium-extend/search/search_chambres.php?&type=chambrehote&codgeo=<?php echo $CODGEO ; ?>&radius=200&lat1="+swLat+"&lng1="+swLng+"&lat2="+neLat+"&lng2="+neLng+"", function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var name = markers[i].getAttribute("name"); var address = markers[i].getAttribute("ville"); var slug = markers[i].getAttribute("slug"); var stars = markers[i].getAttribute("stars"); var type = markers[i].getAttribute("type"); var point = new google.maps.LatLng( parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); var html = "<b>" + name + "</b> " + "<br/>" + "" + "><a href=" + "/chambre-d-hote/" + slug + "/>lien vers la fiche</a>"; var icon = customIcons[type] || {}; //var icon = customIcons[type] || {}; var marker = new google.maps.Marker({ map: map, position: point, icon: icon.icon, shadow: icon.shadow }); bindInfoWindow(marker, map, infoWindow, html); //bounds.extend(point); //map.fitBounds(bounds); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(markers[i].getAttribute("name")); infowindow.open(map, marker); } })(marker, i)); cluster.push(marker); } var mc = new MarkerClusterer(map,cluster,mcOptions); }); } function bindInfoWindow(marker, map, infoWindow, html) { google.maps.event.addListener(marker, 'click', function() { infoWindow.setContent(html); infoWindow.open(map, marker); }); } function downloadUrl(url, callback) { var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest; request.onreadystatechange = function() { if (request.readyState == 4) { request.onreadystatechange = doNothing; callback(request, request.status); } }; request.open('GET', url, true); request.send(null); } function doNothing() {} function changePosition() { var position = document.getElementById('position').value; adUnit.setPosition(google.maps.ControlPosition[position]); } First point : I think I couldn't get the value of swLat, swLng... for passing them to downloadUrl And second point I certainly miss a listener somewhere but don't know where ! Thanks for your inputs ---- EDIT ---- Right now I can load dynamically the data when the idle is changed, but (there is a but) now each change a new marker is add even if it's already on the map. Is there a way to clean all the markers when the idle change before loading the new ones? var cluster = []; var map = new google.maps.Map(document.getElementById("map"), { zoom: 12, center: new google.maps.LatLng('45.7676067','4.8351733'), mapTypeId: google.maps.MapTypeId.ROADMAP }); var mcOptions = {gridSize: 100, styles: [ { textColor: 'black', height: 80, url: "/wp-content/themes/codium-extend/images/cluster-m1.png", width: 90 }, { textColor: 'black', height: 80, url: "/wp-content/themes/codium-extend/images/cluster-m2.png", width: 90 }, { textColor: 'black', height: 80, url: "/wp-content/themes/codium-extend/images/cluster-m3.png", width: 90 } ]}; var infoWindow = new google.maps.InfoWindow; google.maps.event.addListener(map, 'idle', function() { var bounds = map.getBounds(); var swPoint = bounds.getSouthWest(); var nePoint = bounds.getNorthEast(); var swLat = swPoint.lat(); var swLng = swPoint.lng(); var neLat = nePoint.lat(); var neLng = nePoint.lng(); // alert(swLng); downloadUrl("/wp-content/themes/codium-extend/search/search_chambres.php?lat=45.7676067&lng=4.8351733&type=chambrehote&codgeo=<?php echo $CODGEO ; ?>&radius=2000&lat1="+swLat+"&lng1="+swLng+"&lat2="+neLat+"&lng2="+neLng+"", function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var name = markers[i].getAttribute("name"); var address = markers[i].getAttribute("ville"); var slug = markers[i].getAttribute("slug"); var stars = markers[i].getAttribute("stars"); var type = markers[i].getAttribute("type"); var point = new google.maps.LatLng( parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); var html = "<b>" + name + "</b> " + "<br/>" + "" + "><a href=" + "/chambre-d-hote/" + slug + "/>lien vers la fiche</a>"; var icon = customIcons[type] || {}; //var icon = customIcons[type] || {}; var marker = new google.maps.Marker({ map: map, position: point, icon: icon.icon, shadow: icon.shadow }); bindInfoWindow(marker, map, infoWindow, html); //bounds.extend(point); //map.fitBounds(bounds); google.maps.event.addListener(marker, 'click', (function(marker, i) { return function() { infowindow.setContent(markers[i].getAttribute("name")); infowindow.open(map, marker); } })(marker, i)); cluster.push(marker); } var mc = new MarkerClusterer(map,cluster,mcOptions); }); }); function bindInfoWindow(marker, map, infoWindow, html) { google.maps.event.addListener(marker, 'click', function() { infoWindow.setContent(html); infoWindow.open(map, marker); }); } function downloadUrl(url, callback) { var request = window.ActiveXObject ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest; request.onreadystatechange = function() { if (request.readyState == 4) { request.onreadystatechange = doNothing; callback(request, request.status); } }; request.open('GET', url, true); request.send(null); } function doNothing() {} function changeFormat() { var format = document.getElementById('format').value; adUnit.setFormat(google.maps.adsense.AdFormat[format]); } function changePosition() { var position = document.getElementById('position').value; adUnit.setPosition(google.maps.ControlPosition[position]); }
trouble with fusion table map, custom marker and infowindow
I have been working on a fusion table map. I am trying to add custom markers and infowindows but I am having issues getting it to show my markers and infowindows. I think I have something out of order or maybe a few syntax errors I am unable to find. I have sort of confused myself up a bit on the process. I would appreciate any help. var tableId ="1zFwXiOkWbKCY9xRGvXBdHjUkTsirnwbr77WjTds"; // original table google.load('visualization', '1'); var map; var markers = []; var infoWindow = new google.maps.InfoWindow(); function initialize() { var ak = new google.maps.LatLng(64.958056,-147.618333); map = new google.maps.Map(document.getElementById('map_canvas'), { center: ak, zoom: 10, mapTypeId: google.maps.MapTypeId.ROADMAP }); var queryStr = "SELECT name, lat, lng, description FROM "+tableId+" ORDER BY name"; document.getElementById('info').innerHTML += queryStr +"<br>"; var queryText = encodeURIComponent(queryStr); var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + queryText); query.send(getData); } function getData(response) { if (!response) { alert('no response'); return; } if (response.isError()) { alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage()); return; } var dt = response.getDataTable(); document.getElementById('info').innerHTML += "rows="+dt.getNumberOfRows()+"<br>"; for (var i = 0; i < dt.getNumberOfRows(); i++) { var lat = dt.getValue(i,1); var lng = dt.getValue(i,2); var name = dt.getValue(i,0); var description = dt.getValue(i,3); } var pt = new google.maps.LatLng(lat, lng); var html = "<span style='font-size:13px;font-weight:bold;color:#236192;'>" + name + "</span><p style='font-size:12px;'>" + description + "</p>"; var info = html; } function createMarker(pt,info) { var iconURL = 'uaf-icon.png'; var iconSize = new google.maps.Size(29,60); var iconOrigin = new google.maps.Point(0,0); var iconAnchor = new google.maps.Point(15,60); var myIcon = new google.maps.MarkerImage(iconURL, iconSize, iconOrigin, iconAnchor); var shadowURL = 'uaf-shadow.png'; var shadowSize = new google.maps.Size(63, 60); var shadowOrigin = new google.maps.Point(0, 0); var shadowAnchor = new google.maps.Point(15, 60); var myShadow = new google.maps.MarkerImage(shadowURL, shadowSize, shadowOrigin, shadowAnchor); var iconShape = [18,0,20,1,22,2,23,3,24,4,25,5,26,6,27,7,27,8,28,9,28,10,28,11,28,12,28,13,28,14,28,15,28,16,28,17,28,18,28,19,27,20,27,21,26,22,26,23,25,24,24,25,23,26,21,27,20,28,16,29,21,31,21,32,21,33,21,34,21,35,20,36,20,37,20,38,19,39,19,40,19,41,18,42,18,43,18,44,18,45,17,46,17,47,17,48,17,49,16,50,16,51,16,52,15,53,15,54,15,55,14,56,14,57,14,58,14,59,13,59,13,58,13,57,13,56,12,55,12,54,12,53,12,52,11,51,11,50,11,49,11,48,11,47,10,46,10,45,10,44,10,43,9,42,9,41,9,40,9,39,9,38,8,37,8,36,8,35,8,34,8,33,7,32,7,31,12,29,9,28,7,27,6,26,4,25,3,24,3,23,2,22,1,21,1,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,1,9,1,8,2,7,2,6,3,5,4,4,5,3,6,2,8,1,10,0,18,0]; var myMarkerShape = { coord: iconShape, type: 'poly' }; var myMarkerOpts = { position: point, map: map, icon: myIcon, shadow: myShadow, shape: myMarkerShape }; var marker = new google.maps.Marker(myMarkerOpts); markers.push(marker); google.maps.event.addListener(marker, 'click', function() { infoWindow.close(); infoWindow.setContent(info); infoWindow.open(map,marker); }); } function myclick(num) { google.maps.event.trigger(markers[num], "click"); }
first get the map displaying with default markers. You aren't calling the createMarker function (or creating any markers): for (var i = 0; i < dt.getNumberOfRows(); i++) { var lat = dt.getValue(i,1); var lng = dt.getValue(i,2); var name = dt.getValue(i,0); var description = dt.getValue(i,3); var pt = new google.maps.LatLng(lat, lng); createMarker(pt,"<b>"+name+"</b><br>"+description); } there is a typo in createMarker (there is no "point" variable in the scope of the function, it should be "pt"), look for and address any errors in the javascript console. function createMarker(pt,info) { var iconURL = 'uaf-icon.png'; var iconSize = new google.maps.Size(29,60); var iconOrigin = new google.maps.Point(0,0); var iconAnchor = new google.maps.Point(15,60); var myIcon = new google.maps.MarkerImage(iconURL, iconSize, iconOrigin, iconAnchor); var shadowURL = 'uaf-shadow.png'; var shadowSize = new google.maps.Size(63, 60); var shadowOrigin = new google.maps.Point(0, 0); var shadowAnchor = new google.maps.Point(15, 60); var myShadow = new google.maps.MarkerImage(shadowURL, shadowSize, shadowOrigin, shadowAnchor); var iconShape = [18,0,20,1,22,2,23,3,24,4,25,5,26,6,27,7,27,8,28,9,28,10,28,11,28,12,28,13,28,14,28,15,28,16,28,17,28,18,28,19,27,20,27,21,26,22,26,23,25,24,24,25,23,26,21,27,20,28,16,29,21,31,21,32,21,33,21,34,21,35,20,36,20,37,20,38,19,39,19,40,19,41,18,42,18,43,18,44,18,45,17,46,17,47,17,48,17,49,16,50,16,51,16,52,15,53,15,54,15,55,14,56,14,57,14,58,14,59,13,59,13,58,13,57,13,56,12,55,12,54,12,53,12,52,11,51,11,50,11,49,11,48,11,47,10,46,10,45,10,44,10,43,9,42,9,41,9,40,9,39,9,38,8,37,8,36,8,35,8,34,8,33,7,32,7,31,12,29,9,28,7,27,6,26,4,25,3,24,3,23,2,22,1,21,1,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,1,9,1,8,2,7,2,6,3,5,4,4,5,3,6,2,8,1,10,0,18,0]; var myMarkerShape = { coord: iconShape, type: 'poly' }; var myMarkerOpts = { position: pt, map: map, icon: myIcon, shadow: myShadow, shape: myMarkerShape */ }; var marker = new google.maps.Marker(myMarkerOpts); markers.push(marker); google.maps.event.addListener(marker, 'click', function() { infoWindow.close(); infoWindow.setContent(info); infoWindow.open(map,marker); }); } working example (with default icons)
Google Maps API v3 - Arrays and InfoWindows
I'm been trying to make an infowindow for each of my markers for a while now but cant get it to work. This is what I came up with: for(var i=0; i<markery.length; i++) { var latt = parseFloat(markery[i].attributes.getNamedItem("lat").nodeValue); var lon = parseFloat(markery[i].attributes.getNamedItem("lon").nodeValue); var ikona_url = markery[i].attributes.getNamedItem("ikona").nodeValue; var nazwa = markery[i].attributes.getNamedItem("nazwa").nodeValue; var rozmiar = new google.maps.Size(30,23); var punkt_startowy = new google.maps.Point(0,0); var punkt_zaczepienia = new google.maps.Point(15,12); var ikona = new google.maps.MarkerImage(ikona_url, rozmiar, punkt_startowy, punkt_zaczepienia); markert.push(new google.maps.Marker({ position: new google.maps.LatLng(latt,lon), title: nazwa, icon: ikona, map: map, content: nazwa })); google.maps.event.addListener(marker, 'click', function() { var info = new google.maps.InfoWindow({content: this.content}); }); } And the full code is: <script type="text/javascript"> var map; var marker1; var markert = []; var lati; var loni; var infowindow; I start the map: function initialize() { lat = 50.42952; long = 15.60059; var latlng = new google.maps.LatLng(lat, long); var myOptions = { zoom: 5, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP, draggableCursor:'crosshair', }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); dymek = new google.maps.InfoWindow(); google.maps.event.addListener(map, 'click', function(event) { add_marker(event.latLng, 'Your new marker', 'Your new marker' ); }); } This function gets the address of the clicked point: function findAddress(event) { var geocoder = new google.maps.Geocoder(); geocoder.geocode({latLng: event.latLng}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { if (results[0]) { infoWindow.setContent(results[0].formatted_address); infoWindow.setPosition(event.latLng); infoWindow.open(map); } } }); } This function adds a new marker on the map where I clicked: function add_marker( pos, pos_title, pos_str ) { marker1 = new google.maps.Marker( { position: pos, map: map, draggable: true, title: pos_title }); map.setZoom(15); map.setCenter(marker1.getPosition()); LoadMarkers(); } This function loads the nearby points that are near the marker I created in the function above: function LoadMarkers() { var adres='add.xml?lat='+lati+'&long='+loni; jx.load(adres, function(xml) { var markery = xml.getElementsByTagName("marker"); for(var i=0; i<markery.length; i++) { var latt = parseFloat(markery[i].attributes.getNamedItem("lat").nodeValue); var lon = parseFloat(markery[i].attributes.getNamedItem("lon").nodeValue); var ikona_url = markery[i].attributes.getNamedItem("ikona").nodeValue; var nazwa = markery[i].attributes.getNamedItem("nazwa").nodeValue; var markert = addMarkers(latt,lon,ikona_url,nazwa); } },'xml','get'); } This function actually ads the nearby markers on the map. These markers are the ones that I want the infowindow to show: function addMarkers(latt,lon,ikona_url,nazwa) { var rozmiar = new google.maps.Size(30,23); var punkt_startowy = new google.maps.Point(0,0); var punkt_zaczepienia = new google.maps.Point(15,12); var ikona = new google.maps.MarkerImage(ikona_url, rozmiar, punkt_startowy, punkt_zaczepienia); markert.push(new google.maps.Marker({ position: new google.maps.LatLng(latt,lon), title: nazwa, icon: ikona, map: map, animation: google.maps.Animation.DROP })); google.maps.event.addListener(markert, 'tilesloaded', function() { var info = new google.maps.InfoWindow({content: nazwa}); }); }
You're not actually opening the infowindow at any point. Please review this documentation and example, and adapt your code accordingly. You actually need to use the open method for infowindow.