【问题标题】:Google Map API loading dynamic info windowsGoogle Map API 加载动态信息窗口
【发布时间】:2016-08-04 03:07:35
【问题描述】:

我有谷歌地图从我的数据库中动态加载地图上的位置。问题是它没有显示每个位置正确的显示信息。它仅在所有标记上显示上一个产品的信息。我需要以某种方式遍历每个标记。我的代码如下:

<section class="content jumbotron jumbotron-full-height">
    <div class="item slide-1">
        <div id="home-map"></div>
    </div>
</section>

<script>
    var map;
    function initMap() {

        map = new google.maps.Map(document.getElementById('home-map'), {
            center: {lat: 52.1326, lng: 5.2913},
            zoom: 6,
            mapTypeId: 'roadmap',
            styles: [
                {
                    featureType: 'all',
                    stylers: [
                        { saturation: 0 }
                    ]
                },{
                    featureType: 'road.arterial',
                    elementType: 'geometry',
                    stylers: [
                        { hue: '#00ffee' },
                        { saturation: 0 }
                    ]
                },{
                    featureType: 'poi.business',
                    elementType: 'labels',
                    stylers: [
                        { visibility: 'off' }
                    ]
                },{
                    featureType: 'road',
                    elementType: 'labels',
                    stylers: [
                        { visibility: 'on' }
                    ]
                }
            ]
        });

        {% for product in products %}
            var contentString = '<div class="info-window">'+
                                    '<div id="siteNotice">'+
                                    '</div>'+
                                    '<div id="window-content">'+
                                        '{% set path = product.image is not null ? product.image.path|imagine_filter(filter|default('sylius_shop_map_thumbnail')) : 'http://placehold.it/200x200' %}'+
                                        '<img src="{{ path }}" />'+
                                        '<h1>{{ product.code }}</h1>'+
                                    '</div>'+
                                    '<h2>{{ product.city }} - {{ product.country }}</h2>'+
                                    '<p><strong> Description: </strong>{{  product.shortDescription }}</p>'+
                                    '<p><strong> Price: </strong>from {{ product.price|sylius_price }} a day.</p>'+
                                    '<a class="btn btn-primary" href="{{ path("sylius_shop_product_show", {"slug": product.slug}) }}">more info</a>'+
                                '</div>';

            var infowindow = new google.maps.InfoWindow({
                content: contentString,
                maxWidth: 600
            });

            var marker = new google.maps.Marker({
                position: {lat: {{ product.latitude }}, lng: {{ product.longitude }}},
                map: map,
                title: '{{ product.city }}',
                label: '{{ product.address }}'
            });
            marker.addListener('click', function() {
                infowindow.open(map, marker);
            });
        {% endfor %}
    }
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC1c34AXAQ5Y3yV43Alj_ieMRaDPb0qZ44&callback=initMap" async defer>
</script>

【问题讨论】:

    标签: javascript google-maps symfony google-maps-api-3 maps


    【解决方案1】:

    问题是您没有动态设置其内容。为此,您必须从另一个函数创建标记

      <section class="content jumbotron jumbotron-full-height">
            <div class="item slide-1">
                <div id="home-map"></div>
            </div>
        </section>
    
        <script>
            var map,infowindow;
            function initMap() {
    
                map = new google.maps.Map(document.getElementById('home-map'), {
                    center: {lat: 52.1326, lng: 5.2913},
                    zoom: 6,
                    mapTypeId: 'roadmap',
                    styles: [
                        {
                            featureType: 'all',
                            stylers: [
                                { saturation: 0 }
                            ]
                        },{
                            featureType: 'road.arterial',
                            elementType: 'geometry',
                            stylers: [
                                { hue: '#00ffee' },
                                { saturation: 0 }
                            ]
                        },{
                            featureType: 'poi.business',
                            elementType: 'labels',
                            stylers: [
                                { visibility: 'off' }
                            ]
                        },{
                            featureType: 'road',
                            elementType: 'labels',
                            stylers: [
                                { visibility: 'on' }
                            ]
                        }
                    ]
                });
    
                 infowindow = new google.maps.InfoWindow({
                        maxWidth: 600
                    });
    
                {% for product in products %}
                    createMarker(product )
                {% endfor %}
            }
    
           function createMarker(product){
    
            var contentString = '<div class="info-window">'+
                                            '<div id="siteNotice">'+
                                            '</div>'+
                                            '<div id="window-content">'+
                                                '{% set path = product.image is not null ? product.image.path|imagine_filter(filter|default('sylius_shop_map_thumbnail')) : 'http://placehold.it/200x200' %}'+
                                                '<img src="{{ path }}" />'+
                                                '<h1>{{ product.code }}</h1>'+
                                            '</div>'+
                                            '<h2>{{ product.city }} - {{ product.country }}</h2>'+
                                            '<p><strong> Description: </strong>{{  product.shortDescription }}</p>'+
                                            '<p><strong> Price: </strong>from {{ product.price|sylius_price }} a day.</p>'+
                                            '<a class="btn btn-primary" href="{{ path("sylius_shop_product_show", {"slug": product.slug}) }}">more info</a>'+
                                        '</div>';
    
    
                    var marker = new google.maps.Marker({
                        position: {lat: {{ product.latitude }}, lng: {{ product.longitude }}},
                        map: map,
                        title: '{{ product.city }}',
                        label: '{{ product.address }}'
                    });
                    marker.addListener('click', function() {
                        infowindow.setContent(contentString);
                        infowindow.open(map, marker);
                    });
           }
    
    
        </script>
        <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyC1c34AXAQ5Y3yV43Alj_ieMRaDPb0qZ44&callback=initMap" async defer>
        </script>
    

    务必将 infowindow 声明为全局变量

    【讨论】:

    • 它仍然无法工作,因为 createMarker 函数中未定义“产品”,我需要在某处放置一个 {% for product in products %} 以便该函数知道它指的是哪个产品。
    • 哦,那是一个打字错误。我解决了你现在可以试试
    • 第 79 行的 SyliusShopBundle:Homepage:index.html.twig 中仍然出现错误变量“product”不存在 -> 这是第 79 行:'{% set path = product.image is not无效的 ? product.image.path|imagine_filter(filter|default('sylius_shop_map_thumbnail')) : 'placehold.it/200x200' %}'+
    • 你知道为什么它仍然不起作用吗?它仍然说产品未定义。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 2012-05-03
    • 1970-01-01
    相关资源
    最近更新 更多