Solved

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

Posted on 2011-03-02
4
1,024 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
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)

729 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