Skip to main content
Inspiring
March 6, 2016
Question

Array help needed

  • March 6, 2016
  • 1 reply
  • 682 views

HI all,

I think I need some newbie array help.

short story: trying to replace this from html example page:

var locations = [

      ['Bondi Beach', -33.890542, 151.274856, 4],

      ['Coogee Beach', -33.923036, 151.259052, 5],

      ['Cronulla Beach', -34.028249, 151.157507, 3],

      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],

      ['Maroubra Beach', -33.950198, 151.259302, 1]

    ];

 

- with my version from the .each loop:

locations2 += "['id "+ value.id + "'," +value.latitude + "," + value.longitude + ", " + ++cnt + "],"

original link from here:

http://stackoverflow.com/questions/3059044/google-maps-js-api-v3-simple-multiple-marker-example

... the problem i think is I don't know how to make a proper array from my  --- locations2   that I can replace ---  var locations (example) with.

Any Ideas how to make this happen?

code here...

<!doctype html>

<html>

<head>

<meta charset="UTF-8">

<title>Untitled Document</title>

<script src="http://maps.google.com/maps/api/js?sensor=false"

          type="text/javascript"></script>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet" href="inc/jq/themes/theme-02.min.css" />

<link rel="stylesheet" href="inc/jq/themes/jquery.mobile.icons.min.css" />

<link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile.structure-1.4.5.min.css" />

<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>

<script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>

</head>

<body>

<script>

// THIS IS MY database query...

$.post('http://test.com/list.php', {

},

function(data, status){

console.log ('data', data);

var locations2 = [];

var cnt = 0;

$.each(data, function(key, value) {

            console. log ('item', key, value);

locations2 += "['name "+ value.name + "'," +value.latitude + "," + value.longitude + ", " + ++cnt + "],"

});

console.log("loc2: ",locations2);

});

</script>

THIS IS WORKING EXAMPLE PAGE

<div id="map" style="width: 600px; height: 400px;"></div>

<script type="text/javascript">

var locations = [

      ['Bondi Beach', -33.890542, 151.274856, 4],

      ['Coogee Beach', -33.923036, 151.259052, 5],

      ['Cronulla Beach', -34.028249, 151.157507, 3],

      ['Manly Beach', -33.80010128657071, 151.28747820854187, 2],

      ['Maroubra Beach', -33.950198, 151.259302, 1]

    ];

    var map = new google.maps.Map(document.getElementById('map'), {

      zoom: 10,

      center: new google.maps.LatLng(-33.890542,151.274856),

      mapTypeId: google.maps.MapTypeId.ROADMAP

    });

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

  //alert(locations.length)

    for (i = 0; i < locations.length; i++) {

      marker = new google.maps.Marker({

        position: new google.maps.LatLng(locations[1], locations[2]),

        map: map

      });

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

        return function() {

          infowindow.setContent(locations[0]);

          infowindow.open(map, marker);

        }

      })(marker, i));

    }

  </script>

</body>

</html>

    This topic has been closed for replies.

    1 reply

    sinious
    Legend
    March 9, 2016

    The structure of the locations array is "an array of arrays". What you're doing in your jQuery .each() loop is constructing a string rather than an array.

    If the data coming in from your request has the name, latitude and longitude values as your example suggests then it's just about making sure your loop makes "an array or arrays" like the example. e.g.:

    var locations2 = [];

    var cnt = 1;

    $.each( data, function( key, value ) {

    locations2.push(array(

        value.name,

        value.latitude,

        value.longitude,

        cnt++

    ));

    });

    You will then have "an array of arrays" as long as "value" in the "data" you return contains the properties you're using (name, latitude, longitude).