Here is my html:
{% extends 'base.html' %}
{% block title %}Home{% endblock %}
{% block content %}
<style>
/* Set the size of the div element that contains the map */
#map {
height: 700px; /* The height is 400 pixels */
width: 100%; /* The width is the width of the web page */
}
</style>
<!--The div element for the map --> flavor
<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: {'lat': {{ Listing.lat }}, 'lng': {{ Listing.lng }}}, 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=***&callback=initMap">
</script>
{% endblock %}
I need to make the line:
new google.maps.Marker({position: {'lat': {{ Listing.lat }}, 'lng': {{ Listing.lng }}}, map: map});
redirect to /preview/{{ Listing.pk }}
when clicked.
How can I make my marker a clickable link? I've looked at some examples online and the code seems vastly different from mine. Probably because I'm using the Google Maps example code along with some weird Django templating things. Is there a way I can just but my marker inside an tag and put my URL in "href="?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…