[英]Google Maps API JS Marker - Django
In Django, I am trying to populate the google maps api by rendering the template with info from views. 在Django中,我试图通过使用视图中的信息来渲染模板来填充google maps api。
I can get the results to return to the template however I am having difficulty implementing it into the javascript tag with Django. 我可以得到返回到模板的结果,但是我很难用Django将其实现到javascript标签中。 Any suggestions.
有什么建议么。
I would like the django data to return into the addmarker function in JS 我希望Django数据返回到JS中的addmarker函数
def maps_multi(request):
address_list = CustomerOrder.objects.filter(city__icontains = 'CA').distinct() # pulls all unique addresses in California
address_list1 = address_list[0:3] # temporarily select first three addresses from list to test
geolocator = GoogleV3()
add_list=[]
for x in address_list1:
add_city = x.city
location = geolocator.geocode(add_city)
add_list.append(location)
print(add_list)
context = {'location': add_list}
return render(request, 'orderdisplay/maps_multi.html', context)
//JAVASCRIPT
// New map
var map = new google.maps.Map(document.getElementById('map'), options);
// INSERT Lat Lng via django here
addMarker({coords:{lat:33.6846, lng:-117.8265}});
addMarker({coords:{lat:34.0522, lng:-118.2437}});
addMarker({coords:{lat:33.9533, lng:-117.3962}});
// Add Marker Function
function addMarker(props){
var marker = new google.maps.Marker({
position: props.coords,
map:map,
icon:'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png'
});
}
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=*****&callback=initMap">
</script>
I assume your js code is placed within your orderdisplay/maps_multi.html template. 我假设您的js代码位于您的orderdisplay / maps_multi.html模板中。 Then you can simply do this:
然后,您可以简单地执行以下操作:
// New map
var map = ...
// INSERT Lat Lng via django here
{% for location in add_list %}
addMarker({coords:{
lat: {{ location.latitude|stringformat:'f' }},
lng: {{ location.longitude|stringformat:'f' }}
}
});
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.