Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass Django variable as javascript method parameter?
    text
    copied!<p>I have a following <code>extra_context</code> in my views.py.</p> <pre><code>extra_context = { 'user': list_user, 'time': str(list_time), 'latlng': list_latlng } </code></pre> <p>All of the above variables are datatype of <code>python list</code>. Now i want to use these variable in Gmap v3 javascript. I found a following snippet for google map: (posting the main part)</p> <pre><code>&lt;script type="text/javascript"&gt; var map ; function initialize() { var latlng = new google.maps.LatLng(-34.397, 150.644); var myOptions = { zoom: 2, center: latlng, mapTypeId: google.maps.MapTypeId.TERRAIN }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var flightPlanCoordinates = [ {% for ip in latlng %}{{ ip }}{% endfor %}]; for(var i in flightPlanCoordinates){ var marker = add_marker(flightPlanCoordinates[i][0], flightPlanCoordinates[i][1],"Some title!","html for info box"); marker.setMap(map); } } function add_marker(lat,lng,title,box_html) { var infowindow = new google.maps.InfoWindow({ content: box_html }); var marker = new google.maps.Marker({ position: new google.maps.LatLng(lat,lng), map: map, title: title }); google.maps.event.addListener(marker, 'click', function() { infowindow.open(map,marker); }); return marker; </code></pre> <p>}</p> <p>The above code shows a marker accoriding to the <code>latlng</code> list passed from the context. But the problem is occurring at that time when i also tried to pass <code>time and user</code> with the same way. It shows nothing on the Map. By the way i tried with this coide:</p> <pre><code>var user = [{% for i in user %}{{ i }}{% endfor %}]; var timestamp = [{% for y in time %}{{ y }}{% endfor %}]; </code></pre> <p>Is that the right way? I am new in javascript i wrote according the <code>latlng</code> code. </p> <p>I tried with <code>alert(timestamp)</code>. Nothing happens :(</p> <p>Another problem is i have to pass these three parameter to <code>add_marker</code> java script method. TO do this i have to run three for loop concurrently. Posting the pseudo code:</p> <pre><code>var user = [{% for i in user %}{{ i }}{% endfor %}]; var timestamp = [{% for y in time %}{{ y }}{% endfor %}]; var flightPlanCoordinates = [ {% for ip in latlng %}{{ ip }}{% endfor %}]; for (var in flightPlanCoordinates) and for (a in timestamp) and for(b in user) { var marker = add_marker(flightPlanCoordinates[i][0], flightPlanCoordinates[i][1],b,a); marker.setMap(map); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload