Javascript Object Oriented Programming / Google Maps

Posted on 2011-03-12
Last Modified: 2012-05-11
I'm implementing a google map in my site.  It takes addresses, sends them to the geocoder, which translates the address into a point, and then markers and infowindows are created for each point.  The problem I'm having is when I set the event listener for the markers, it is always using the latest instance of the location.  I don't know how to "pass" a parameter into the add listener function.  So when you click a marker, it brings up the infowindow and pans the map to the location like it should, but the infowindow content always reads the last address' content (for all points).  It's like it is getting overridden everytime.
function codeAddress() {

for (count = 0; count < addrArr.length; count++) {

addr = addrArr[count];
thisflag = flagArr[count];

	if (geocoder) {

		geocoder.geocode( { 'address': addr}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {

			point = results[0].geometry.location;
			/* set bounds */

			var loc = new addresslocation(point, addr, thisflag);

this variable needs to be inside the addListener function  --->      iw = loc.getInfoWin();
          var mark = loc.getMark();

google.maps.event.addListener(mark, 'click', function() {  

			if (openobj != null) openobj.closeWin();
			openobj = loc;




Open in new window

function addresslocation(point, address, flagcontent) {

this.point = point;
this.address = address;
this.flagcontent = flagcontent;

	this.mark = new google.maps.Marker({
				map: map,
				position: this.point,
				title: this.address
	this.infowin = new google.maps.InfoWindow({
				content: this.flagcontent,
				maxWidth: 275
this.getMark = function() { return this.mark };
this.getInfoWin = function() { return this.infowin };
this.closeWin = function() { this.infowin.close(); };


Open in new window

Question by:jpking72
  • 3
  • 2

Expert Comment

ID: 35120204
Well, it is being told to use the contents of the flagArr as the content of the InfoWindows.  Can you please share where the flagArr is being populated?

Your answer lies there.
LVL 29

Accepted Solution

Badotz earned 500 total points
ID: 35121926
>>it is always using the latest instance of the location

It could be because you use a loop to process your locations, and the last one is the only one that remains visible.

Expert Comment

ID: 35156098
LVL 29

Expert Comment

ID: 35156620

Open in new window

LVL 29

Expert Comment

ID: 35342299
No worries - glad to help.

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 remove string values 2 29
how can i count words? 2 40
how to handle long text strings in javascript or ajax 1 25
Web forms being filled out by bots 4 31
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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)
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…

815 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

6 Experts available now in Live!

Get 1:1 Help Now