MVC4 asp.net访问kml文件 [英] MVC4 asp.net accessing kml file in view

查看:126
本文介绍了MVC4 asp.net访问kml文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在visual studio 2012的MVC4项目中遇到了一个问题。
我在symfony2中构建了一个后台,并且可以显示带有从kml文件加载的多边形的Google地图。

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.

现在我正在尝试在asp.net(C#)中做同样的事情......但是我的代码似乎无法访问kml文件。

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...

我在我的项目中创建了一个目录命名为'Kmls',我粘贴我的kml(1.kml,2.kml,...)
这是我的代码:

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>

当我在firefox中运行应用程序(调试)时,在控制台中出现以下错误:

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()); 
}

任何帮助将不胜感激!

推荐答案

我认为你的问题可能是为了在Google Maps中加载KML文件(至少对于免费版本),它需要是Web Accessible(即您提供的URL需要通过网络访问,而不仅仅是本地开发/生产服务器)。因此/Kmls/10.kml不会是一个有效的网址,而是类似于 http:// myserver / KML 是必需的。

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.

如下面的评论所提供的,这里是一个类似的回答问题的链接: Google Maps API和KML文件LocalHost开发选项

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

这篇关于MVC4 asp.net访问kml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆