0

Here is my requirement

Need to create array like this.I'm use Spring mvc + ajax + jquery + google map 3.0 I'm get these values seperately as json response.

1. city name
2. long
3. lat
var markers = [['Bondi Beach', -33.890542, 151.274856],['Coogee Beach', -33.923036, 151.259052],['Cronulla Beach', -34.028249, 151.157507],['Manly Beach', -33.80010128657071, 151.28747820854187],['Maroubra Beach', -33.950198, 151.259302]];

how to create above loop with javascript array functions.may be records more than 20,30 is it 3D array.?

please help me to sort out this issue

thanks all

below show my script code and help me to modify that to get my target result

function jsonResponse(){
    var longitude=0;
    var latitude=0;
    var merchant='';
    var total =0;

    $.ajax({ 
        url: 'merchantsList.html', 
        dataType: 'json', 
        contentType: 'application/json',
        mimeType: 'application/json',
        success: function(data) {
            longitude=data.longitude;
            latitude=data.latitude;
            merchant=data.merchant;
            total = data.length;
        },
        error:function(data,status,er) { 
            alert("error: "+data+" status: "+status+" er:"+er);
        }
    });

    var markers = [['Bondi Beach', -33.890542, 151.274856],['Coogee Beach', -33.923036, 151.259052],
                   ['Cronulla Beach', -34.028249, 151.157507],['Manly Beach', -33.80010128657071, 151.28747820854187],
                   ['Maroubra Beach', -33.950198, 151.259302]];
    var infowindow = new google.maps.InfoWindow(), marker, i;
    for (i = 0; i < markers.length; i++) {
        marker = new google.maps.Marker({
            position: new google.maps.LatLng(latitude, longitude),
            map: map
            });}
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infowindow.setContent(markers[i][0]);
            infowindow.open(map, marker);
        };
    })(marker, i));

}
2
  • 2
    It's not clear what you're asking. You want to create a nested array (note, there aren't any multidimensional arrays in JS), but then you provide exactly that in your code. What is it you are having problems with? Commented Feb 18, 2014 at 12:41
  • thanks dear Andy.below solutions helped me @ those are works :-) Commented Feb 19, 2014 at 3:11

3 Answers 3

1

Some wild assumptions: var markers is sample data, and you want to replace that with what is returned by the ajax query; city name == merchant in the ajax data.

If the data coming back from merchantsList.html has multiple merchant/long/lat items, you'll need to iterate over data and add each to the markers array.
That would be

var markers = [];

// ajax call start here..
    // ...
    success: function(data) {
        var i, item;
        for (i = 0; i < data.length; i++) {
            item = data[i];
            markers.push([item.merchant, item.longtitude, item.latitude]);
        }
    },
// ajax call end code here
0
1

Create multidimension array in javascript

var iMax = 20;
var jMax = 10;

var f = new Array();

for (i=0;i<iMax;i++) {

 f[i]=new Array();

 for (j=0;j<jMax;j++) {

  f[i][j]=0;

 }

}
1
  • all of your answers help me a lot.thanks dear Tamizh :-) Commented Feb 19, 2014 at 3:15
1

I'm assuming you're talking about how do you iterate over your markers... You currently do this:

var infowindow = new google.maps.InfoWindow(), marker, i;
for (i = 0; i < markers.length; i++) {
    marker = new google.maps.Marker({
        position: new google.maps.LatLng(latitude, longitude),
        map: map
        });
}

but this won't work: new google.maps.LatLng(latitude, longitude). You need to get the lat and long from the current marker:

 position: new google.maps.LatLng(markers[i][1], markers[i][2]),
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.