[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 444
  • Last Modified:

Javascript Object Oriented Programming / Google Maps

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 */
			bounds.extend(point);
			map.fitBounds(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() {  
map.setZoom(15);

		        iw.open(map,mark);
			map.panTo(mark.getPosition());
			if (openobj != null) openobj.closeWin();
			openobj = loc;
			});

locArray.push(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

0
jpking72
Asked:
jpking72
  • 3
  • 2
1 Solution
 
WebmonkeyCommented:
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.
0
 
BadotzCommented:
>>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.
0
 
WebmonkeyCommented:
Bump
0
 
BadotzCommented:
Bump
bump

Open in new window

.
0
 
BadotzCommented:
No worries - glad to help.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now