Solved

Javscript - get Marker to Click From Sidebar Link, as well as Map?

Posted on 2011-03-02
4
1,013 Views
Last Modified: 2012-05-11
Hello, I have an application that places markers on a map for selected schools and opens an info window abou the school when clicked. I'm also putting the school names in a sidebar as hyperlinks, and want the same info window to open when the link is clicked, but I am having trouble referencing it correctly in the myclick function shown in the code. There are probably other things that need to be cleaned up as well. Thanks for the help.

<html>
<head>
<!---                              Google Map                           --->
<meta name="viewport" content="initial-scale=1.0, user-scalable=yes" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var infowindow;
var arrMarkers = new Array();

MapMarker = function(lat,lng,m_title,m_content) {
  this.lat = lat;
  this.lng = lng;
  this.m_title = m_title;
  this.m_content = m_content;
}

<cfoutput query="GetSchools">
  arrMarkers[#GetSchools.currentrow-1#] = new MapMarker(#GetSchools.newlat#,#GetSchools.newlong#,"Title","#GetSchools.school#");
</cfoutput>

initialize = function(){
  var myLatlng = new google.maps.LatLng(38.52525, -121.51870);
  // this variable will collect the html which will eventually be placed in the side_bar
  var side_bar_html = "";
  // arrays to hold copies of the markers and html used by the side_bar
  // because the function closure trick doesnt work there


  var myOptions = {
    scrollwheel: true,
    zoom: 12,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
  // Close InfoWindow when clicking anywhere on the map.
  infoWindow = new google.maps.InfoWindow();
  google.maps.event.addListener(map, 'click', function() {
    infoWindow.close();
  });

 
  // draw markers on map
  for (var i in arrMarkers) {
    var marker = new google.maps.Marker(
		{   position: new google.maps.LatLng(arrMarkers[i].lat, arrMarkers[i].lng),
      		map: map,
      		title: arrMarkers[i].m_title
    	}
		);
	  
	  // Register a click listener on each marker created
    google.maps.event.addListener(marker, 'click', (function(markerArg, index) 
	  {
      return function() 
	  	{
        infoWindow.setContent(arrMarkers[index].m_content);
        infoWindow.open(map, markerArg);
      	};
      })(marker, i));
  
 
     side_bar_html += '<a href="javascript:myclick(' + (arrMarkers.length-1) + ')">' + arrMarkers[i].m_content + '<\/a><br>';
 
	   // put the assembled side_bar_html contents into the side_bar div
       document.getElementById("side_bar").innerHTML = side_bar_html;
	}
 }

    // This function picks up the click and opens the corresponding info window
       function myclick(i) {
        google.maps.event.trigger(arrMarkers[i], "click");
      	}    
   
      
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>

<body>
  	<div> 
			<h2><a href="../">School Maps</a></h2> 
	</div> 
		
		<div id="controlbar"> 
		</div> 
    <div id="map_canvas" style="width: 550px; height: 570px; float:left;"></div>
    <div id="side_bar" title="Schools" style="width: 230px; height: 570px;  float:left;  background-color: silver;"  >
      <h2 style="text-align: center;">Schools</h2>
 
     <span style="text-align: left;  display:inline;">&nbsp;  <br></span> 
 
    </div>
 
 </body>
</html>  
 

Open in new window

0
Comment
Question by:medsoos
  • 2
4 Comments
 
LVL 15

Expert Comment

by:Gurpreet Singh Randhawa
ID: 35024618
Can you Show me the link i will try to check what is wrong
0
 
LVL 13

Expert Comment

by:Molnar István
ID: 35024869
here is an example that can be useful:
http://econym.org.uk/gmap/example_map2.htm (http://econym.org.uk/gmap/basic2.htm)

you can  create an xml file or use a db to store the coords:
http://econym.org.uk/gmap/basic3.htm - read xml and display markers and sidebar menu
0
 
LVL 13

Accepted Solution

by:
Molnar István earned 500 total points
ID: 35025824
i corrected your code, but the code is not the cleanest
check if it works for you, i uploaded the file below (save.html)

hope it works for you too
save.html
0
 
LVL 1

Author Closing Comment

by:medsoos
ID: 35029617
Quicksilver, thanks so much. Works great!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now