点击添加按钮后添加多个谷歌地图

时间:2015-01-30 10:57:11

标签: jquery google-maps symfony twig

我正在使用Symfony 2.3,twig和这个插件location picker来显示地图。我嵌入了一组地址。每个地址表都有一张地图。用户可以手动添加地址表单。然后谷歌地图显示正确的第一个地址,但添加一个借调地址时,地图不会显示。 chrome控制台不显示任何错误。

vigilance_point_collection-prototype.html.twig文件:

{% block vigilance_point_collection_widget %}
    {% spaceless %}
        {# <style>
             .remove{
                 position: absolute;
                 top: -36px;
                 right: -48px;
             }
            /* .element-div{
                 display: inline-block;
                 margin-bottom: -10px;
                 position: relative;
             }*/
         </style>#}
        <div class="collection">
            {% if prototype is defined %}
                {% set attr = attr|merge({'data-prototype': block('vigilance_point_collection_item_widget') }) %}
            {% endif %}
            <div {{ block('widget_container_attributes') }}>
                {{ form_errors(form) }}
                {% for rows in form %}
                    <div class="element-div">
                        {% set fieldNum = 1 %}
                        {% for row in rows %}
                            <div class="form-group field{{ fieldNum }}">
                                {{ form_label(row) | trans | raw }}
                                {{ form_errors(row) }}
                                <div class="col-lg-10">
                                    {{ form_widget(row) }}
                                </div>
                            </div>
                            {% set fieldNum = fieldNum + 1 %}
                        {% endfor %}
                        <a class="remove btn" title="Supprimer" href="javascript:void(0)">
                            <img src="http://icons.iconarchive.com/icons/custom-icon-design/flatastic-1/24/delete-1-icon.png">
                        </a>
                        <div class="clear"></div>
                    </div>
                {% endfor %}
                {{ form_rest(form) }}
            </div>
            <div class="clear"></div>
            <a class="add{{ id }}" title="{{ 'button.add' | trans | capitalize }}" href="javascript:void(0);">
                <img src="http://icons.iconarchive.com/icons/custom-icon-design/flatastic-1/24/add-1-icon.png">
            </a>
        </div>
        <script type="text/javascript">
            var elementCount{{ id }} = '{{ form | length }}';
            jQuery(document).ready(function () {
                jQuery('.add{{ id }}').click(function () {
                    var elementList = jQuery('#{{ id }}');
                    var newWidget = elementList.attr('data-prototype');
                    newWidget = newWidget.replace(/{{ form.vars.name }}___name__/g, "{{ form.vars.name }}_" + elementCount{{ id }});
                    newWidget = newWidget.replace(/{{ form.vars.name }}]\[__name__/g, "{{ form.vars.name }}][" + elementCount{{ id }});
                    elementCount{{ id }}++;
                    jQuery('#{{ id }}').append(newWidget);
                    $('#us3').locationpicker({
                        location: {latitude: 46.15242437752303, longitude: 2.7470703125},
                        inputBinding: {
                            latitudeInput: $('#mybook_adminbundle_outlet_addresses_0_latitude'),
                            longitudeInput: $('#mybook_adminbundle_outlet_addresses_0_longitude'),
                            radiusInput: $('#mybook_adminbundle_outlet_addresses_0_phone'),
                            locationNameInput: $('#mybook_adminbundle_outlet_addresses_0_location')
                        },
                        enableAutocomplete: true,
                        onchanged: function (currentLocation, radius, isMarkerDropped) {
                            // Uncomment line below to show alert on each Location Changed event
                            //alert("Location changed. New location (" + currentLocation.latitude + ", " + currentLocation.longitude + ")");
                        }
                    });
                    return false;
                });
            });
            jQuery(document).ready(function () {
                jQuery('.remove').livequery('click', function (e) {
                    jQuery(this).parent('.element-div').fadeOut().remove();
                    return false;
                });
            });
        </script>  
    {% endspaceless %}
{% endblock vigilance_point_collection_widget %}

{% block vigilance_point_collection_item_widget %}
    {% spaceless %}
        {% set fieldNum = 1 %}
        <div class="element-div collection">
            <div id='us3' style='width: 550px; height: 400px;'></div>
            {% for row in prototype %}
                <div class="form-group">
                    {{ form_label(row) | trans | raw }}
                    {{ form_errors(row) }}
                    <div class="col-lg-10">
                        {{ form_widget(row) }}
                    </div>
                </div>
                {% set fieldNum = fieldNum + 1 %}
            {% endfor %}
            <a class="remove btn" title="Supprimer" href="javascript:void(0);">
                <img src="http://icons.iconarchive.com/icons/custom-icon-design/flatastic-1/24/delete-1-icon.png">
            </a>
        </div>
    {% endspaceless %}
{% endblock vigilance_point_collection_item_widget %}

1 个答案:

答案 0 :(得分:0)

我猜的问题在于:

$('#us3').locationpicker({

... 

<div id='us3' style='width: 550px; height: 400px;'></div>
{% for row in prototype %}

所以你有一个你正在加载地图的div。您可能需要为每个地址在不同的div上调用.locationpicker()构造函数,并在每个地址的for循环内创建一个唯一命名的div。 e.g。

$('#us{{ id }}').locationpicker({

... 

{% for row in prototype %}
    <div id='us{{ id }}' style='width: 550px; height: 400px;'></div>