Solved

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

Posted on 2011-03-02
4
1,008 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
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)

759 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

25 Experts available now in Live!

Get 1:1 Help Now