【问题标题】:Customized google maps marker icon not showing in partial view自定义谷歌地图标记图标未在部分视图中显示
【发布时间】:2012-03-27 15:16:24
【问题描述】:

发生了一些奇怪的事情(至少对我而言)。我有一个显示几个谷歌地图标记的部分视图。但是,一旦我使用自己的自定义图标,如果拒绝显示。但是当我在 global.asax.cs 的 RegisterRoutes 函数中给出我的路由时,例如

   routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

如果我的部分视图在索引视图中,它出于我未知的原因决定显示自己? 为什么会这样以及如何解决?

更新(代码): 这是我在名为 MapResults 的局部视图中的代码。

<script type="text/javascript">

    var geocoder;
    var map;
    function initialize() {
        geocoder = new google.maps.Geocoder();
        var latlng = new google.maps.LatLng(-34.397, 150.644);
        var myOptions = {
            zoom: 8,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        collectingData();
        google.maps.event.addListener(map, 'click', function() {

        });      
   } 

    function codeAddress(location, name, contract) {
        var address = location;
        var infowindow;
        var image;
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                    map.setCenter(results[0].geometry.location);

                     var image = new google.maps.MarkerImage(returnImage(contract),
                      // This marker is 20 pixels wide by 32 pixels tall.
                      new google.maps.Size(30, 42),
                      // The origin for this image is 0,0.
                      new google.maps.Point(40,45),
                      // The anchor for this image is the base of the flagpole at 0,32.
                      new google.maps.Point(0, 32));

                    var marker = new google.maps.Marker({
                    map: map,
                  //  icon: image,   only showing when registered in global asax
                    position: results[0].geometry.location,            
                });

          infowindow = new InfoBox(infoboxOptions(boxText(name, contract)));
                     google.maps.event.addListener(marker, 'click', function() {
                    infowindow.open(map, marker);
                     });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }   
        });               
    }

这些是我最重要的功能。

这是一个使用我的局部视图的视图

@model IPagedList<HospitalSearch.Models.Buildings>

@{
   ViewBag.Title = "Map";

}
         <div>     
            @Html.Partial("MapResults")
        </div>

  @foreach (var item in Model) {
        <input id="adres" type="hidden" value=@Html.DisplayFor(modelItem => item.StreetBuilding) @Html.DisplayFor(modelItem => item.NumberBuilding) />  
  }

  </body>
<div class="pager">
    @Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount)
</div>

将它放在 RegisterRoutes 中会按需要显示,否则不会。 更新: 事实证明,RegisterRoutes 以某种方式获取了我的图像的“正确”url。如果我使用直接路径说http://localhost:8080/Content/image.png,那么一切都很好。 但是“Content/image.png”不起作用。如果我弄清楚到底发生了什么,我当然会更新。

【问题讨论】:

    标签: javascript asp.net-mvc google-maps razor google-maps-api-3


    【解决方案1】:

    当您处于局部视图中时,您的自定义标记的图像路径可能不再有效。你是如何引用你的图像的?最好使用 Url.Content 辅助方法来获取路径。

    如果这不能解决问题,您可以发布更多代码吗? HTML和地图javascript?谢谢。

    【讨论】:

    • 感谢您的回复,让我更新我的帖子,我测试了代码,但它没有在部分视图中并且仍然无法正常工作。
    • returnImage 是一个单独的 javascript 函数吗?
    猜你喜欢
    • 1970-01-01
    • 2014-07-18
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 2015-06-06
    • 2018-01-13
    相关资源
    最近更新 更多