/    Sign up×
Community /Pin to ProfileBookmark

I had this working this morning and then I started working on getting different colored markers. Suddenly things failed. I removed my color markers script and thought I had this code back to where I was but, [I]marker[/I] isn’t being the Array I had as evidence by the markers off/on button and when ALL markers are shown. Individually, I have the markers opening the infoWindow as they should. What happened?! The trouble is in function MK() specifically I think, at line 40. Originally, I had assembled the markers with the listener as the page was loading, now I have it in function but,,, what happened?

[code=html]<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”X-UA-Compatible” content=”IE=EmulateIE7″/>
<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″/>
<meta name=”viewport” content=”initial-scale=1.0, user-scalable=no”/>

<title>Ee</title>
<style type=”text/css”>
#map_b{width:640px; height:480px;position:absolute;top:2px;left:2px; }
#sBar{text-align:center;width:100px;position:absolute;right:1px;top:1px}
</style>

<script src=”http://maps.google.com/maps?file=api&v=2&key=” type=”text/javascript”></script>
<script type=”text/javascript”>
// lat long zoom infoWindow index
var ps=[‘37.4220323, -120.0845109,4,All,0’,
‘37.4220323, -122.0845109,14,PaloAlto,1’,
‘39.4220323, -120.0845109,7,Anywhere,2’]

for(i=0;i<ps.length;i++){ps[i]=ps[i].split(‘,’)}
var marker=[]; var h=[];

function mapIt(za,zb,zc,zd,ze){
map = new GMap2(document.getElementById(“map_b”));
var point = new GLatLng(za, zb);
map.setCenter(point, zc);
map.setMapType(G_HYBRID_MAP);
map.setUIToDefault();
map.enableRotation();
if(zd==’All’){MK(); return}
else{
marker=new GMarker(new GLatLng(ps[ze][0],ps[ze][1])); h=”+ps[ze][3]+”;
GEvent.addListener(marker, ‘click’, function() { marker.openInfoWindowHtml(h); });
map.addOverlay(marker);}
}
function MK(){ marker=[]; h=[];
for(x=1;x<ps.length;x++){
marker[x]=new GMarker(new GLatLng(ps[x][0],ps[x][1])); h[x]=”+ps[x][3]+”;
GEvent.addListener(marker[x], ‘click’, function() { marker[x].openInfoWindowHtml(h[x]); });
map.addOverlay(marker[x])
}
}
function pointsOnOff(x){if(x.value==’off’){for(i=0;i<marker.length;i++){map.removeOverlay(marker[i]);}x.value=’on’;} else {for(i=1;i<marker.length;i++){map.addOverlay(marker[i]);} x.value=’off’}}

</script>
</head>
<body onload=”mapIt(ps[0][0],ps[0][1],5,’All’,0)” onunload=”GUnload()”>
<div id=”map_b” ></div>
<div id=”sBar” >
<script type=”text/javascript”>
for(i=0;i<ps.length;i++){document.write(‘<input type=button value=”‘+ps[i][3]+'” onclick=”mapIt(‘+ps[i][0]+’,’+ps[i][1]+’,’+ps[i][2]+’,this.value,’+i+’)”><br>’)}
</script>
<br>Markers<input type=button onclick=”pointsOnOff(this)” value=”off”>
</div>

</body>

</html>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@tirnaJun 11.2010 — Have you got a backup of the file at the stage it was working?

If so, it might be quicker to just restore it and continue from there than try to debug the current file.

Just some food for thought - if you don't already do so, backup the project at regular intervals (especially at critical stages) so that if things go awry in the future you can always easily go back to a previous working version and continue on from there.
Copy linkTweet thisAlerts:
@justinbarneskinauthorJun 12.2010 — Thanks, Trina. You might notice the title, 5th in a series of backups.

Unfortunately, shid happens.
Copy linkTweet thisAlerts:
@astupidnameJun 12.2010 — One problem I see, backreferencing the loop counter variable [x] is not correct way to go about it as x will be equal to the length of the array after the loop has finished running, and there will be no marker[x] then when the onclick function is actually invoked by user. A good way to fix it is to do one of a couple things. Either build the event handler in a function which returns a function, or attach properties to the soft javascript objects and then use 'this'. So one fix:
function MK(){
for(var marker, x=1;x&lt;ps.length;x++){
marker=new GMarker(new GLatLng(ps[x][0],ps[x][1]));
marker.html_info = ''+ps[x][3]+'';
GEvent.addListener(marker, 'click', function() { this.openInfoWindowHtml(this.html_info); });
map.addOverlay(marker);
}
}


Alternatively:
function MK(){
function clck(mrkr, html) {
return function () {
mrkr.openInfoWindowHtml(html);
};
}
for(var marker, x=1;x&lt;ps.length;x++){
marker=new GMarker(new GLatLng(ps[x][0],ps[x][1]));
GEvent.addListener(marker, 'click', clck(marker, ''+ps[x][3]));
map.addOverlay(marker);
}
}
Copy linkTweet thisAlerts:
@justinbarneskinauthorJun 12.2010 — Thank you so much. Before I lose it again, here it is...
[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>

<title>Aa</title>
<style type="text/css">
#map_b{width:640px; height:480px;position:absolute;top:2px;left:2px; }
#sBar{text-align:center;width:100px;position:absolute;right:1px;top:1px}
</style>

<script src="http://maps.google.com/maps?file=api&v=2&key=" type="text/javascript"></script>
<script type="text/javascript">
// lat long zoom infoWindow index
var ps=['37.4220323, -121.0845109,5,All,0',
'37.4220323, -122.0845109,14,PaloAlto,1',
'39.4220323, -120.0845109,7,Anywhere,2',
'40.4220323, -119.0845109,7,somewhere,3']

for(i=0;i<ps.length;i++){ps[i]=ps[i].split(',')}

function mapIt(za,zb,zc,zd,ze){
map = new GMap2(document.getElementById("map_b"));
var point = new GLatLng(za, zb);
map.setCenter(point, zc);
map.setMapType(G_HYBRID_MAP);
map.setUIToDefault();
map.enableRotation();

if(zd=='All'){for(i=1;i<marker.length;i++){map.addOverlay(marker[i]);} return;}
else{ map.addOverlay(marker[ze]); return; }
}
var marker=[]
for( x=1;x<ps.length;x++){
marker[x]=new GMarker(new GLatLng(ps[x][0],ps[x][1]));
marker[x].html_info = ''+ps[x][3]+'';
GEvent.addListener(marker[x], 'click', function() { this.openInfoWindowHtml(this.html_info); });
}


function pointsOnOff(t){if(t.value=='off'){for(i=1;i<marker.length;i++){map.removeOverlay(marker[i]);} t.value='on';} else {for(i=1;i<marker.length;i++){map.addOverlay(marker[i]);} t.value='off'}}

</script>
</head>
<body onunload="GUnload()">
<div id="map_b" ></div>
<div id="sBar" >
<script type="text/javascript">
for(i=0;i<ps.length;i++){document.write('<input type=button value="'+ps[i][3]+'" onclick="mapIt('+ps[i][0]+','+ps[i][1]+','+ps[i][2]+',this.value,'+i+')"><br>')}
</script>
<br>Markers<input type=button onclick="pointsOnOff(this)" value="off">
</div>

</body>


</html>
[/code]
×

Success!

Help @justinbarneskin spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...