Solved

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

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i get the length? 7 32
How to make a moving character 3 57
javascript comparing two dates 3 18
Restore of mysql database from .SQL file - using Coldfusion 5 36
Hi. There are several upload tutorials using jquery and coldfusion. I found a very interesting one here Upload Your Files using Jquery & ColdFusion and Preview them (http://www.randhawaworld.com/) . I did keep the main js functions but made sever…
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 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…

776 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