简体   繁体   English

此页面上的Google Maps API多次-Modal Bootstrap Return Json Success

[英]Google Maps API multiple times on this page - Modal Bootstrap Return Json Success

The problem is when I open modal bootstrap which carries Google Maps. 问题是当我打开带有Google Maps的模式引导程序时。

Modal Bootstrap - _Edit.cshtml 模态引导-_Edit.cshtml

<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">Editar</h4>
</div>

        @using (Ajax.BeginForm("Edit", "Account", new AjaxOptions { HttpMethod = "POST", OnSuccess = "onModalSuccess" }, new { @id = "ModalformId", @class = "form-horizontal", role = "form" }))

        {
@Html.AntiForgeryToken()
    <div class="modal-body">
        <div class="form-horizontal">
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                        <div class="form-group">

                            <div class="col-xs-6">
                                @Html.LabelFor(m => m.GoogleMaps_Link)
                                @Html.TextBoxFor(m => m.GoogleMaps_Link, new { @id = "pac-input", @class = "controls", @readonly = true })
                                <br />
                                <div id="map-canvas" class="Help_GoogleMaps" style="width:865px;height:380px;" title="Edit"></div>
                            </div>
                        </div>

                   <div class="modal-footer">
                        <input type="submit" class="btn btn-success" value="Save" />
                        <button type="button" class="btn btn-danger" data-dismiss="modal">Close</button>
                    </div>
            </div>
    </div>



                               @*Success Message Modal*@
                    <div id="ModalMsgBoxId" class="modal fade" tabindex="-1" role="dialog">
                        <div class="modal-dialog modal-sm">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                        <span aria-hidden="true">×</span>
                                    </button>
                                    <h4 class="modal-title">
                                        <strong id="ModalTitleId" style="margin-left: 6px; font-size: 16px;"></strong>
                                    </h4>
                                </div>
                                <div class="modal-body">
                                    <p>
                                        <div id="ModalContentId" style="margin-left: 6px; font-size: 16px;"></div>
                                    </p>
                                </div>
                                    <div class="modal-footer">
                                        <button id="normalOkId" type="button" class="btn btn-success" data-dismiss="modal">OK</button>
                                    </div>
                                </div>
                            </div>
                        </div>
    }

    // Google Maps Search

    <script>

                $(document).on("shown.bs.modal", function () {
                        if (window.google && window.google.maps) {
                            initAutocomplete();
                            return;
                        }
                        $script = $("<script>",
                                    {
                                        'type': 'text/javascript',
                                        'src': 'https://maps.googleapis.com/maps/api/js?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&libraries=places&callback=initAutocomplete'
                                    });
                        $script.appendTo($("head"));
                    });

                    function initAutocomplete() {
                        var LatLong = @Html.Raw(JsonConvert.SerializeObject(Model.Geo));
                        var LatLongSplit = LatLong.split(" ");
                        var lat = LatLongSplit[0];
                        var long = LatLongSplit[1];

                        //var Lat = (-23.5326148);
                        //var Long = (-46.803688);
                        var Endereco = @Html.Raw(JsonConvert.SerializeObject(Model.GoogleMaps_Link));

                        var map = new google.maps.Map(document.getElementById('map-canvas'), {
                            center: new google.maps.LatLng(lat, long),
                            zoom: 11,
                            scrollwheel: false,
                            mapTypeId: google.maps.MapTypeId.ROADMAP
                        });

                        var markerLatLong = new google.maps.Marker({
                            position: new google.maps.LatLng(lat, long),
                            map: map,
                            title: Endereco
                        });

                        // Create the search box and link it to the UI element.
                        var input = document.getElementById('pac-input');
                        var searchBox = new google.maps.places.SearchBox(input);
                        map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

                        // Bias the SearchBox results towards current map's viewport.
                        map.addListener('bounds_changed', function () {
                            searchBox.setBounds(map.getBounds());
                        });

                        //Clear Markers
                        var markers = [];
                        // [START region_getplaces]
                        // Listen for the event fired when the user selects a prediction and retrieve
                        // more details for that place.
                        searchBox.addListener('places_changed', function () {
                            var places = searchBox.getPlaces();

                            if (places.length == 0) {
                                return;
                            }
                            markerLatLong.setMap(null);
                            // Clear out the old markers.
                            markers.forEach(function (marker) {
                                marker.setMap(null);
                            });
                            markers = [];

                            // For each place, get the icon, name and location.
                            var bounds = new google.maps.LatLngBounds();
                            places.forEach(function (place) {
                                var icon = {
                                    url: place.icon,
                                    size: new google.maps.Size(71, 71),
                                    origin: new google.maps.Point(0, 0),
                                    anchor: new google.maps.Point(17, 34),
                                    scaledSize: new google.maps.Size(25, 25)
                                };

                                // Create a marker for each place.
                                markers.push(new google.maps.Marker({
                                    map: map,
                                    title: place.name,
                                    position: place.geometry.location
                                }));

                                if (place.geometry.viewport) {
                                    // Only geocodes have viewport.
                                    bounds.union(place.geometry.viewport);
                                } else {
                                    bounds.extend(place.geometry.location);
                                }
                            });
                            map.fitBounds(bounds);
                        });
                        // [END region_getplaces]
                    }

var onModalSuccess = function(result)  
{  
    if (result.EnableError)  
    {  
        // Clear.   
        $('#ModalTitleId').html("");  
        $('#ModalContentId').html("");  
        // Setting.   
        $('#ModalTitleId').append(result.ErrorTitle);  
        $('#ModalContentId').append(result.ErrorMsg);  
        // Show Modal.   
        $('#ModalMsgBoxId').modal(  
        {  
            backdrop: 'static',  
            keyboard: false  
        });  
    }  
    else if (result.EnableSuccess)  
    {  
        // Clear.   
        $('#ModalTitleId').html("");  
        $('#ModalContentId').html("");  
        // Setting.   
        $('#ModalTitleId').append(result.SuccessTitle);  
        $('#ModalContentId').append(result.SuccessMsg);  
        // Show Modal.   
        $('#ModalMsgBoxId').modal(  
        {  
            backdrop: 'static',  
            keyboard: false  
        });  
        // Resetting form.   
        $('#ModalformId').get(0).reset();  
    }  
} 

    </script>

Controller 调节器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(EditModel model)
    {
        if (ModelState.IsValid)
        {
            return this.Json(new { EnableSuccess = true, SuccessTitle = "Success", SuccessMsg = "Success" });
        }
retur PartialView (_Edit, model);
    }

The problem is in partial view. 问题是局部的。 Just show a warning! 只是显示警告!

With the code below: 使用以下代码:

<div id="ModalMsgBoxId" class="modal fade" tabindex="-1" role="dialog">
                            <div class="modal-dialog modal-sm">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                            <span aria-hidden="true">×</span>
                                        </button>
                                        <h4 class="modal-title">
                                            <strong id="ModalTitleId" style="margin-left: 6px; font-size: 16px;"></strong>
                                        </h4>
                                    </div>
                                    <div class="modal-body">
                                        <p>
                                            <div id="ModalContentId" style="margin-left: 6px; font-size: 16px;"></div>
                                        </p>
                                    </div>
                                    <div class="modal-footer">
                                        <button id="normalOkId" type="button" class="btn btn-success" data-dismiss="modal">OK</button>
                                    </div>
                                </div>
                            </div>
                        </div>

Picture: 图片:

在此处输入图片说明

The problem is modal Success, does it make Google Maps fail. 问题是模式成功,是否会使Google Maps失败。 How can I fix? 我该如何解决?

Thank you guys. 感谢大伙们。

I solved the problem in another way. 我用另一种方式解决了这个问题。 Using Bootstrap Alert. 使用Bootstrap Alert。

Link: Alerts - Bootstrap 链接: 警报-引导程序

Remove code: 删除代码:

<div id="ModalMsgBoxId" class="modal fade" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-sm">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
                <h4 class="modal-title">
                    <strong id="ModalTitleId" style="margin-left: 6px; font-size: 16px;"></strong>
                </h4>
            </div>
            <div class="modal-body">
                <p>
                    <div id="ModalContentId" style="margin-left: 6px; font-size: 16px;"></div>
                </p>
            </div>
            <div class="modal-footer">
                <button id="normalOkId" type="button" class="btn btn-success" data-dismiss="modal">OK</button>
            </div>
        </div>
    </div>
</div>

New code: 新代码:

<center><div id="result"></div></center>

javascript: JavaScript的:

$("#result").html('<div class="alert alert-success"><button type="button" class="close">×</button>Successfully updated record!</div>');

Now it works! 现在可以了! I took example link: here 我以示例链接为例: 这里

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同一页面上的多个Google Maps在引导弹出模式下不起作用 - multiple google maps on same page not working in bootstrap popup modal 您已在此页面上多次包含Google Maps API - You have included the Google Maps API multiple times on this page “您在此页面上多次包含 Google Maps API”错误 - "You have included the Google Maps API multiple times on this page" Error Bootstrap Modal中带有多个标记的Google Maps - Google Maps with multiple markers in Bootstrap Modal 无法在Bootstrap Modal中加载Google Maps JavaScript API - Cannot get Google Maps JavaScript API to load within Bootstrap Modal 通过Google Maps API显示Bootstrap登录模式 - Show Bootstrap login modal over Google Maps API 试图多次加载角度+在此页面上多次包含Google Maps API - Tried to load angular more than once + included the Google Maps API multiple times on this page 您已在此页面上多次包含Google Maps API错误尽管我没有包含 - You have included the Google Maps API multiple times on this page Error Although i have'nt include 您已在此页面上多次包含 Google 地图 JavaScript API。 这可能会导致意外错误 - You have included the Google Maps JavaScript API multiple times on this page. This may cause unexpected errors 您在此页面上多次包含 Google Maps API。 这可能会导致意外错误(wordpress) - you have included the Google Maps API multiple times on this page. This may cause unexpected errors (wordpress)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM