简体   繁体   中英

Displaying more than one Google Map V3 on child templates

I have a problem displaying multiple google maps on child templates. Only first initialized map in the script is displaying, second seems to be ignored.

If I place two maps on one child template everything works fine, but adding second map to an additional template is making second map not working.

base.html :

{% block body %}
<h1>Base</h1>
{% endblock %}

<script>
function initMap() {
    var uluru = {lat: 12, lng: 23};
    var map = new google.maps.Map(document.getElementById('map'), {
      zoom: 6,
      center: uluru
    });

    var uluru2 = {lat: 13, lng: 24};
    var map2 = new google.maps.Map(document.getElementById('map2'), {
      zoom: 6,
      center: uluru2
    });

child.html :

{% extends 'base.html' %}

{% block body %}
<div id="map"></div>
{% endblock %}

child2.html :

{% extends 'base.html' %}

{% block body %}
<div id="map2"></div>
{% endblock %}

Unfortunately only solution I found is to split map initialization through child templates.

child.html :

{% extends 'base.html' %}

{% block body %}
<div id="map"></div>
{% endblock %}

<script>
    function initMap() {
        var uluru = {lat: 12, lng: 23};
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 6,
          center: uluru
    });
</script>

child2.html :

{% extends 'base.html' %}

{% block body %}
<div id="map2"></div>
{% endblock %}

<script>
    function initMap() {
        var uluru2 = {lat: 13, lng: 24};
        var map2 = new google.maps.Map(document.getElementById('map2'), {
          zoom: 6,
          center: uluru2
    });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM