Why Not
Why Not

Reputation: 611

Loop issue with Google Map markers

Trying to place markers on a Google Map from a Django backend using the lat and long for each object. Having a very frustrating time doing so. I believe the problem is within the for loop but I can't determine exactly what the issue is (I may just need a second loop). Any aid or insight would be very appreciated.

var stories = [{% for story in stories %}
        {latitude:{{story.latitude}},longitude:{{story.longitude}}}, {% endfor %}];




function loadMarkers(stories){
    for (i=0;i<stories.length;i++) {
        var story = stories[i];
        var point = new google.maps.LatLng(story.latitude, story.longitude);
        var marker = new google.maps.Marker({position: point, map: map});
    }
}

loadMarkers(stories);

Upvotes: 0

Views: 95

Answers (2)

simon xu
simon xu

Reputation: 982

i suggest you check two points:

1)django => there maybe a problem about {{ and }}

2)js = > the last ',' cause an error,you cant make an object like this :{lat:1,lng:2,}

Upvotes: 1

Vinyl Windows
Vinyl Windows

Reputation: 503

I am not familiar with Django, but if your issues is the pure javaScript code I can offer the first place that I would look.

Check to make sure that you're looping an array and not an object. Every object in javascript has a length property.

Do the below in some debugger like in FF or chrome.

function loadMarkers(stories){
for (i=0;i<stories.length;i++) {
    var story = stories[i];
    var point = new google.maps.LatLng(story.latitude, story.longitude);
    var marker = new google.maps.Marker({position: point, map: map});

console.log(i);
console.log(stories);
console.log(stories[i]);
}

}

Upvotes: 2

Related Questions