Referencing Google Maps API markers from Javascript

Posted on 2013-06-17
Last Modified: 2013-06-17
Here is my test page:

It works fine. If you click any of the red markers on the map, you get a details "info window". The click event is coded like this:

google.maps.event.addListener(marker0,'click', function(){
	infowindow.setContent('<p><b>Play It Again Sports - Wyomissing</b><br />1050 Berkshire Blvd, Wyomissing, PA 19610<br />United States<br />610.779.8360<br />Distance: 5 miles or 9 kilometers</p>');,marker0);

Open in new window

However, I would like to also trigger these "info window" events from other elements. For example, I setup a test button on that same page (you will see it at the top-right of the screen):

<input id="mybutton" type="button" value="click here" />

Open in new window

I tried handling that button like this:

		infowindow.setContent('content here');,marker0);

Open in new window

...but I just get an error "cannot find the variable 'infowindow'".

How can I make it so clicking that button displays one of the five info windows on that screen? I guess I am not sure how to reference the info window, outside of the code that Google Maps API has already provided.

Thank you!
Question by:bbdesign
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

Accepted Solution

Ersoy Hasan earned 500 total points
ID: 39252933
You can't find infowindow variable because it is defined inside the initialize function's scope and you are trying to access it outside it. Move this :
			infowindow.setContent('content here');,marker0);

Open in new window

inside the initialize function.

Author Comment

ID: 39252959
You are correct, that fixed it. Thanks!

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

630 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