MVC4 asp.net accessing kml file in view

白昼怎懂夜的黑 提交于 2019-12-11 08:16:38

问题


I have a problem in my MVC4 project in visual studio 2012. I build a backoffice in symfony2 and there i can display google maps with polygons loaded from a kml file.

Now i am trying to do the same thing in asp.net (C#)... But my code doesn't seem able to access the kml-file.

I am not using the Model View Controller system to acces my file, and maybe that is the problem...

i made a directory in my project named 'Kmls' and i pasted my kml in there (1.kml, 2.kml, ...) this is my code of my view:

@model IEnumerable<SocialGeo.Models.district>
@{
   ViewBag.Title = "Index";
}

<h3>Choose a district first</h3>

<ul class="allDistricts" data-kml="">
    @foreach (var item in Model) {
        @Html.ActionLink(item.district_name, "DistrictNews", new { id=item.district_id})
    }
</ul>

<script type="text/javascript">
  $(document).ready(function () {
    var myOptions = {
        center: new google.maps.LatLng(51.087633, 3.711569),
        zoom: 11,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }



    var paths =
    {
        imagePath: "@Url.Content("~/Kmls/10.kml")"
    }


    $("section").append('<div class="mapscanvas" id="map_canvas' + 0 + '"></div>');

    map = new google.maps.Map(document.getElementById("map_canvas" + 0), myOptions);

    geoXml = geoXML3.parser({ map: map, singleInfoWindow: true, zoom: false });
    //var jej = @HttpContext.Current.Server.MapPath("~/Kmls/10.kml")

    geoXml.parse(paths.imagePath);

});
</script>

When i run the application (debugging) in firefox i get the following error in console:

GET: http://localhost:53760/Kmls/10.kml 404NotFound
HTTP error 404 retrieving /Kmls/10.kml in polys.js (line 990)
Unable to retrieve /Kmls/10.kml

I see the map, but not the polygon that I need.

Do I have to use a controller for this too? this is my controlleraction that is being called:

public ActionResult Index()
{
    var districts = db.districts.Include(d => d.city);
    return View(districts.ToList()); 
}

Any help would be highly appreciated!


回答1:


I think your problem may be that in order to load a KML file in Google Maps (at least for the free version), it needs to be Web Accessible (i.e. the URL you provide needs to be accessible from the web, not just locally on your development / production server). Therefore "/Kmls/10.kml" would not be a valid URL and something like "http://myserver/KML" is required.

As provided in the comments below, here is a link to a similar answered question: Google Maps API and KML File LocalHost Development Options



来源:https://stackoverflow.com/questions/14183353/mvc4-asp-net-accessing-kml-file-in-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!