我使用gmaps.geocode python库来生成此代码:
[{'address_components': [{'long_name': '900', 'short_name': '900', 'types': ['street_number']}, {'long_name': 'West Wall Street', 'short_name': 'W Wall St', 'types': ['route']}, {'long_name': 'Janesville', 'short_name': 'Janesville', 'types': ['locality', 'political']}, {'long_name': 'Rock County', 'short_name': 'Rock County', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Wisconsin', 'short_name': 'WI', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '53548', 'short_name': '53548', 'types': ['postal_code']}, {'long_name': '3574', 'short_name': '3574', 'types': ['postal_code_suffix']}], 'formatted_address': '900 W Wall St, Janesville, WI 53548, USA', 'geometry': {'location': {'lat': 42.6803769, 'lng': -89.03211}, 'location_type': 'RANGE_INTERPOLATED', 'viewport': {'northeast': {'lat': 42.6817258802915, 'lng': -89.0307610197085}, 'southwest': {'lat': 42.6790279197085, 'lng': -89.03345898029151}}}, 'place_id': 'Eig5MDAgVyBXYWxsIFN0LCBKYW5lc3ZpbGxlLCBXSSA1MzU0OCwgVVNBIhsSGQoUChIJFzIpc5QZBogRoK3T0RxPudkQhAc', 'types': ['street_address']}]
使用Django,我可以为数据库中保存的每个列表吐出这些数据。在我的地图上显示每个经过地理编码的地址的最佳方法是什么: html:
{% extends 'base.html' %}
{% block title %}Home{% endblock %}
{% block content %}
<style>
/* Set the size of the div element that contains the map */
#map {
height: 400px; /* The height is 400 pixels */
width: 100%; /* The width is the width of the web page */
}
</style>
<!--The div element for the map -->
<div id="map"></div>
<script>
// Initialize and add the map
function initMap() {
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 4, center: {'lat': 42.6803769, 'lng': -89.03211}}});
{% for Listing in posts %}
new google.maps.Marker({position: {{ Listing.geo }}, map: map});
{% endfor %}
}
</script>
<!--Load the API from the specified URL
* The async attribute allows the browser to render the page while the API loads
* The key parameter will contain your own API key (which is not needed for this tutorial)
* The callback parameter executes the initMap() function
-->
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&callback=initMap">
</script>
{% endblock %}
我当前的代码应该将每个地址输出为地图上的标记,但是我的地图甚至没有显示出来,所以我显然弄坏了一些东西。前端不是我的最爱,所以请告诉我我做错了什么以及如何完成正确的输出。
{{Listing.geo}}输出我显示的第一个代码段。
当我的html在浏览器中输出时,我的脚本如下:
<script>
// Initialize and add the map
function initMap() {
var map = new google.maps.Map(
document.getElementById('map'), {zoom: 4, center: {'lat': 42.6803769, 'lng': -89.03211}});
new google.maps.Marker({position: [{'address_components': [{'long_name': '900', 'short_name': '900', 'types': ['street_number']}, {'long_name': 'West Wall Street', 'short_name': 'W Wall St', 'types': ['route']}, {'long_name': 'Janesville', 'short_name': 'Janesville', 'types': ['locality', 'political']}, {'long_name': 'Rock County', 'short_name': 'Rock County', 'types': ['administrative_area_level_2', 'political']}, {'long_name': 'Wisconsin', 'short_name': 'WI', 'types': ['administrative_area_level_1', 'political']}, {'long_name': 'United States', 'short_name': 'US', 'types': ['country', 'political']}, {'long_name': '53548', 'short_name': '53548', 'types': ['postal_code']}, {'long_name': '3574', 'short_name': '3574', 'types': ['postal_code_suffix']}], 'formatted_address': '900 W Wall St, Janesville, WI 53548, USA', 'geometry': {'location': {'lat': 42.6803769, 'lng': -89.03211}, 'location_type': 'RANGE_INTERPOLATED', 'viewport': {'northeast': {'lat': 42.6817258802915, 'lng': -89.0307610197085}, 'southwest': {'lat': 42.6790279197085, 'lng': -89.03345898029151}}}, 'place_id': 'Eig5MDAgVyBXYWxsIFN0LCBKYW5lc3ZpbGxlLCBXSSA1MzU0OCwgVVNBIhsSGQoUChIJFzIpc5QZBogRoK3T0RxPudkQhAc', 'types': ['street_address']}], map: map});
}
</script>
这对我来说似乎不正确。
Views.py
def home(request):
posts = Listing.objects.all().filter(is_live=1)
context = {'posts': posts}
return render(request, 'home.html', context)
@login_required
def post(request):
if request.method == "POST":
form = ListingForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
geo = gmaps.geocode(post.street_address + ", " + post.city + ", " + post.state)
post.save()
return redirect('post_view', pk=post.pk)
else:
form = ListingForm()
return render(request, 'post.html', {'form': form})
答案 0 :(得分:0)
从第一个代码段来看,您应该使用{{ Listing[0].geometry.location|safe }}
而不是{{ Listing.geo }}
来打印出非转义的json对象