Solved

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

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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
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…
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…

749 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