Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #27148

javascript loop problem

Newsgroups comp.lang.javascript
Date 2015-03-31 19:05 -0700
Message-ID <8f8b8bbd-fed1-4871-b3e4-e0b96abd3692@googlegroups.com> (permalink)
Subject javascript loop problem
From chrisb@ctlsoft.com

Show all headers | View raw


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?address=' + 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>

Back to comp.lang.javascript | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

javascript loop problem chrisb@ctlsoft.com - 2015-03-31 19:05 -0700
  Re: javascript loop problem Richard Maher <maher_rjSPAMLESS@hotmail.com> - 2015-04-01 10:49 +0800
  Re: javascript loop problem "Christoph M. Becker" <cmbecker69@arcor.de> - 2015-04-01 14:42 +0200
  Re: javascript loop problem Denis McMahon <denismfmcmahon@gmail.com> - 2015-04-01 14:43 +0000

csiph-web