【问题标题】:Opening Google Maps inside a Partial View with JSON使用 JSON 在局部视图中打开 Google 地图
【发布时间】:2013-01-13 09:17:00
【问题描述】:

我正在尝试使用 JSON 在部分视图中显示 Google 地图。我已经在普通视图中尝试过代码,它工作得很好。

我有以下几点:-

局部视图 ShowMap.cshtml

@using Microsoft.Web.Helpers

<script src="~/Scripts/jquery-1.8.3.min.js"></script>
<div class="experienceRestrictedText">
    @Maps.GetGoogleHtml("1, Redmond Way, Redmond, WA", width: "400", height: "400")
</div>

Index.cshtml(部分视图的启动位置)

    $('.modal_link_map').on('click', function (e) {
        $('.modal_part').show();
        var id = $(this).attr('data-id');
        var context = $('#tn_select').load('/Experience/ShowMap?id=' + id, function () {
            initSelect(context);
        });
        e.preventDefault();
        return false;
    });

而控制器Action如下:-

        public ActionResult ShowMap()
        {
        _ItemID = Convert.ToInt32(Request.QueryString["id"]);

        viewModel.ExperienceViewModel.Experience =     unitOfWork.ExperienceRepository.GetByID(_ItemID);

        return PartialView(viewModel);
        }

我是否需要添加任何其他内容才能使此地图正常工作?

【问题讨论】:

    标签: c# asp.net-mvc razor google-maps-api-3 partial-views


    【解决方案1】:

    我不熟悉 @Maps.GetGoogleHtml 助手,但我担心这个助手包含以下脚本:

    <script src="//maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
    

    除了这个脚本不能异步加载,因为它使用document.write()来加载实际的API。要完成这项工作,您应该指定一个callback 参数。以下是您的部分内容的样子:

    <div class="experienceRestrictedText">
        <script src="//maps.google.com/maps/api/js?sensor=false&callback=initialize" type="text/javascript"></script>
        <script type="text/javascript">
            function initialize() {
                var map = new google.maps.Map(document.getElementById("map"), { zoom: 14, center: new google.maps.LatLng(47.652437, -122.132424), mapTypeId: google.maps.MapTypeId['ROADMAP'] });
                new google.maps.Geocoder().geocode({ address: '1, Redmond Way, Redmond, WA' }, function (response, status) {
                    if (status === google.maps.GeocoderStatus.OK) {
                        var best = response[0].geometry.location;
                        map.panTo(best);
                        new google.maps.Marker({ map: map, position: best });
                    }
                });
            }
        </script>
        <div class="map" id="map" style="width:400px; height:400px;"></div>
    </div>
    

    注意回调参数是如何传递给http://maps.google.com/maps/api/js 脚本的,地图的实际初始化是在这个回调中完成的。

    还请注意,我已从部分中删除了 jquery 脚本。我猜 jQuery 已经加载到您的布局中,因为您似乎正在使用它来附加到 .on() 处理程序。

    作为替代方案,您可以在主视图中包含 &lt;script src="//maps.google.com/maps/api/js?sensor=false" type="text/javascript"&gt;&lt;/script&gt;,但一旦加载部分内容, @Maps.GetGoogleHtml helper 会再次包含它。

    就我个人而言,我不是这类助手的忠实拥护者,因为它们完全掩盖了潜在的调用,使您无法控制和理解正在发生的事情。

    【讨论】:

    • 嗨,达林,是的,这行得通。如何更改地址以从我的模型中传递动态地址?
    • 很容易。你的模型中的一个字符串属性,你将在控制器操作中填充它,然后简单地使你的局部视图强类型化到这个视图模型,并使用模型中的值:{ address: @Html.Raw(Json.Encode(Model.Address)) } 传递给geocode 函数。
    • 太棒了!我试过@Html.Raw(Model.Address) 但没有用,这就是我问的原因。非常感谢,现在一切正常!
    • 你看我的评论了吗? @Html.Raw(Model.Address) 是错误的并且非常不安全。永远不要使用这样的东西。如果你想正确编码值,你应该使用@Html.Raw(Json.Encode(Model.Address))
    • 嗨,Darin,我做到了,这就是为什么我将其更改为 Json.Encode!以前不知道这个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 2013-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-07-27
    相关资源
    最近更新 更多