I need to display multiple markers on a map, each with their own infowindow. I have created the individual markers without a problem, but don't know how to create the infowindows for each.
I am generating a map using the V3 API within an ASP-based website, with markers being created from a set of DB records. The markers are created by looping through a rs and defining a marker() with the relevant variables:
var myLatlng = new google.maps.LatLng(lat,long);
var marker = new google.maps.Marker({
map: map,
position: myLatlng,
title: 'locationname',
icon: 'http://google-maps-icons.googlecode.com/files/park.png'
});
This is creating all the relevant markers in their correct locations.
What I need to do now, and am not sure of how to achieve is give each of them their own unique infowindow which I can use to display information and links relevant to that marker.
Source
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script language="javascript">
$(document).ready(function() {
//Google Maps
var myOptions = {
zoom: 5,
center: new google.maps.LatLng(-26.66, 122.25),
mapTypeControl: false,
mapTypeId: google.maps.MapTypeId.ROADMAP,
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<!-- While locations_haslatlong not BOF.EOF -->
<% While ((Repeat1__numRows <> 0) AND (NOT locations_haslatlong.EOF)) %>
var myLatlng = new google.maps.LatLng(<%=(locations_haslatlong.Fields.Item("llat").Value)%>,<%=(locations_haslatlong.Fields.Item("llong").Value)%>);
var marker = new google.maps.Marker({
map: map,
position: myLatlng,
title: '<%=(locations_haslatlong.Fields.Item("ldescription").Value)%>',
icon: 'http://google-maps-icons.googlecode.com/files/park.png',
clickable: true,
});
<%
Repeat1__index=Repeat1__index+1
Repeat1__numRows=Repeat1__numRows-1
locations_haslatlong.MoveNext()
Wend
%>
<!-- End While locations_haslatlong not BOF.EOF -->
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
google.maps.event.addListener(marker, 'dblclick', function() {
map.setZoom(14);
});
});
Here is something that works (RoR / Rails / Ruby on Rails):
There was a problem in the VB/... code that was answered before, the infowindow object must be unique per clickable marker. Demo on: http://www.geodepollution.org/
I tried the code from Sean Vieira, I was struggling to get the infowindow show me same corresponding markers Info.
If you have same problem try: Here we are setting info (you can keep any name ) and then latter will use it.
next see Content, so we are using the info we want to set to Marker
This worked for me.
Ref: https://tommcfarlin.com/multiple-infowindows-google-maps/
This seems pretty good.
http://www.usnaviguide.com/v3maps/v3multipleinfowindows.htm
The problem is in your call to
addListener()
While you loop through your recordset and write out the javascript again and again and again and again for adding a marker to the map, you only call the event listener once. Also, you never actually create any objects of the InfoWindow type, so you never have anything to call
open()
on.The quick and dirty solution is to add this after you create your
marker
but before you end yourWhile
loop.But don't do this -- you're using VB to write totally redundant Javascript for you, when you could be using VB to fetch what you need, and then let Javascript do the work that you need done with the data.
The better way to do what you are trying to do is to rewrite your VB to create an array of Javascript objects, each one containing a park's information. Then use Javascript to loop over that array and set up the markers and their associated infoWindows for you.
Outlining the proposed solution:
Here is a great solution.
http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/