[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

javascript loop problem

chrisb

4/1/2015 2:06:00 AM

little script which adds map markers to the appropriate locations with a label.
The array 'myaddresses' contain the locations to mark on the map and the numbers to put on the labels.
Works fine for the location bit, but the labels all show the last count (35).

So the line labelContent: myaddresses[x][1] always seems to return the final array element for some reason.

Any ideas greatly appreciated!


<script type='text/javascript'>
$(window).load(function(){
$(document).ready(function () {
var myOptions = {
zoom: 6,
center: new google.maps.LatLng(-42.397, 172.644),
mapTypeId: 'terrain'
};
var map = new google.maps.Map($('#map_canvas')[0], myOptions);

var myaddresses = [['Cambridge', 100], ['Auckland',20], ['Wellington',1], ['Taupo',35]];

for (var x = 0; x < myaddresses.length-1; x++) {

$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?add... + myaddresses[x][0] + '&sensor=false', null, function (data) {
try {

var p = data.results[0].geometry.location;
var latlng = new google.maps.LatLng(p.lat, p.lng);
var marker1 = new MarkerWithLabel({
position: latlng,
draggable: false,
raiseOnDrag: false,
title: myaddresses[x][0],
map: map,
labelContent: myaddresses[x][1],
labelAnchor: new google.maps.Point(12, 50),
labelClass: "labels",
labelStyle: { opacity: 1.0 }

});

google.maps.event.addListener(marker1);
}
catch (err) { }


});
}

});
});

</script>
3 Answers

Richard Maher

4/1/2015 2:49:00 AM

0

On 4/1/2015 10:05 AM, chrisb@ctlsoft.com wrote:
> little script which adds map markers to the appropriate locations with a label.
> The array 'myaddresses' contain the locations to mark on the map and the numbers to put on the labels.
> Works fine for the location bit, but the labels all show the last count (35).
>
> So the line labelContent: myaddresses[x][1] always seems to return the final array element for some reason.
>
> Any ideas greatly appreciated!

1) Stop using that jQuery shit!!!

2) Load the maps with
google.load('maps', '3', {
'other_params' :
'sensor=true&language=' + params.lang,
'callback' : mapsLoaded
});

3) What the hell is MarkerWithLabel and where does it com from?

4) FWIW, InfoWindow.setContent() does not render the change a(nother)
InfoWindow.Open() is needed to see the results.

5) Use InfoBubble instead!!!

6) Don't know what that crap getJSON does but I'm guessing you're
specifying a callback that fires after the loop (once for each of your
calls. Javascript is single-threaded. Have to leave your function to
allow further events in). "X" has it's last value.

7) You need a closure (actually a redesign but . . .) use function(inCntr){
var x = inCntr;
function(){
// your function referencing x
}
}(x);

8) I lived in Wellington and Christchurch for 18 months. Shame about
CHCH :-(

Christoph M. Becker

4/1/2015 12:43:00 PM

0

chrisb@ctlsoft.com wrote:

> little script which adds map markers to the appropriate locations with a label.
> The array 'myaddresses' contain the locations to mark on the map and the numbers to put on the labels.
> Works fine for the location bit, but the labels all show the last count (35).
>
> So the line labelContent: myaddresses[x][1] always seems to return the final array element for some reason.

When this expression is evaluated, x === myaddresses.length-1.

> <script type='text/javascript'>
> $(window).load(function(){
> $(document).ready(function () {

That seems clumsy at best. You'll want to skip registering an event
listener for window's load event, here.

> var myOptions = {
> zoom: 6,
> center: new google.maps.LatLng(-42.397, 172.644),
> mapTypeId: 'terrain'
> };
> var map = new google.maps.Map($('#map_canvas')[0], myOptions);
>
> var myaddresses = [['Cambridge', 100], ['Auckland',20], ['Wellington',1], ['Taupo',35]];
>
> for (var x = 0; x < myaddresses.length-1; x++) {

Don't use a for loop here, but rather $.each().

--
Christoph M. Becker

Denis McMahon

4/1/2015 2:43:00 PM

0

On Tue, 31 Mar 2015 19:05:54 -0700, chrisb wrote:

> var myaddresses = [['Cambridge', 100], ['Auckland',20],
> ['Wellington',1], ['Taupo',35]];
>
> for (var x = 0; x < myaddresses.length-1; x++) {
> /* do stuff */
> }

Let's step through the loop.
myaddresses.length = 4

1st time round. x starts as 0. x is less than 3. 1 is added to x. x is
now 1. The loop body executes with x = 1, myaddresses[x] =
['Auckland',20].

2nd time round. x starts as 1. x is less than 3. 1 is added to x. x is
now 2. The loop body executes with x = 1, myaddresses[x] =
['Wellington',1].

3rd time round. x starts as 2. x is less than 3. 1 is added to x. x is
now 3. The loop body executes with x = 1, myaddresses[x] = ['Taupo',35].

4th time round. x starts as 3. x is not less than 3. loop terminates.

You might like to look at the (admittedly non jquery) page here:

http://www.sined.co.uk/tmp/ta...

--
Denis McMahon, denismfmcmahon@gmail.com