python - How to pass Django variable as javascript method parameter? -
i have following extra_context
in views.py.
extra_context = { 'user': list_user, 'time': str(list_time), 'latlng': list_latlng }
all of above variables datatype of python list
. want use these variable in gmap v3 javascript. found following snippet google map: (posting main part)
<script type="text/javascript"> 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 = [ {% ip in latlng %}{{ ip }}{% endfor %}]; for(var in flightplancoordinates){ var marker = add_marker(flightplancoordinates[i][0], flightplancoordinates[i][1],"some title!","html 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;
}
the above code shows marker accoriding latlng
list passed context. problem occurring @ time when tried pass time , user
same way. shows nothing on map. way tried coide:
var user = [{% in user %}{{ }}{% endfor %}]; var timestamp = [{% y in time %}{{ y }}{% endfor %}];
is right way? new in javascript wrote according latlng
code.
i tried alert(timestamp)
. nothing happens :(
another problem have pass these 3 parameter add_marker
java script method. have run 3 loop concurrently. posting pseudo code:
var user = [{% in user %}{{ }}{% endfor %}]; var timestamp = [{% y in time %}{{ y }}{% endfor %}]; var flightplancoordinates = [ {% ip in latlng %}{{ ip }}{% endfor %}]; (var in flightplancoordinates) , (a in timestamp) , for(b in user) { var marker = add_marker(flightplancoordinates[i][0], flightplancoordinates[i][1],b,a); marker.setmap(map);
you need define variables as:
var user = [{% in user %}{{ }}{% if forloop.last %} {%else%} , {%endif%} {% endfor %}];
i.e. add comma between values in array.
Comments
Post a Comment