代码之家  ›  专栏  ›  技术社区  ›  Mohsen

将Geojson加载到美联社.NETMVC 5

  •  0
  • Mohsen  · 技术社区  · 6 年前

    它使用geojson文件:“states.geojson".

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>
        body {font: 12px sans-serif;}
        path {
            stroke-width: 1px;
            stroke: white;
            fill: #804040;
            cursor: pointer;
        }
        path:hover, path.highlighted {
            fill: #ff8000;
        }
    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script>
    
    //Map dimensions (in pixels)
    var width = 600,
        height = 350;
    
    //Map projection
    var projection = d3.geo.albersUsa()
        .scale(730.2209486090715)
        .translate([width/2,height/2]) //translate to center the map in view
    
    //Generate paths based on projection
    var path = d3.geo.path()
        .projection(projection);
    
    //Create an SVG
    var svg = d3.select("body").append("svg")
        .attr("width", width)
        .attr("height", height);
    
    //Group for the map features
    var features = svg.append("g")
        .attr("class","features");
    
    //Create zoom/pan listener
    //Change [1,Infinity] to adjust the min/max zoom scale
    var zoom = d3.behavior.zoom()
        .scaleExtent([1, Infinity])
        .on("zoom",zoomed);
    
    svg.call(zoom);
    
    d3.json("states.geojson",function(error,geodata) {
        if (error) return console.log(error); //unknown error, check the console
    
    //Create a path for each map feature in the data
    features.selectAll("path")
        .data(geodata.features)
        .enter()
        .append("path")
        .attr("d",path)
        .on("click",clicked);
    });
    
    function zoomed() {
        features.attr("transform", "translate(" + zoom.translate() + ")
        scale("+ zoom.scale() + ")")
       .selectAll("path").style("stroke-width", 1 / zoom.scale() + "px" );
    }
    
    </script>
    </body>
    

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            string path = GetGisPath() + "\\states.geojson";
            ViewBag.Path = path.Replace("\\", "/");
            return View();
        }
    
        private static string GetGisPath()
        {
            string appPath = System.Web.Hosting.HostingEnvironment.MapPath("~/App_Data");
            return appPath;
        }
    }
    

    然后是具有名称索引的视图。我复制了视图中html文件的内容并更改了代码:

    d3.json("states.geojson",function(error, geodata)
    

    收件人:

    d3.json("@ViewBag.Path", function (error, geodata)
    

    但什么也没发生。我只在Firefox控制台中看到一个错误:

    XMLHttpRequest { onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, responseURL: "", status: 0, statusText: "", responseType: "", response: "" }
    

    与以下代码相关:

    d3.json("@ViewBag.Path", function (error, geodata) {
        if (error) return console.log(error); //unknown error, check the console
        ...
    

    我在“查看页面源代码”中看到了下面的代码:

    d3.json("C:/Users/XXXXXXXX/Documents/Visual Studio 2013/Projects/ASP.NET/proj/proj/App_Data/states.geojson", function (error, geodata) { ...
    

    也用于:

    string path = GetGisPath() + "\\states.geojson";
    ViewBag.Path = path;
    return View();
    

    在“查看页面源”中看到:

    d3.json("C:\Users\XXXXXXXX\Documents\Visual Studio 2013\Projects\ASP.NET\proj\proj\App_Data\states.geojson", function (error, geodata) { ...
    

    有什么建议吗?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Mohsen    6 年前

    好 啊。我没有找到加载Geojson文件的解决方案。

    更新:

    经过多次尝试,下面的代码应该添加到Web.config文件(不是web.config文件).

    <system.webServer>
      <staticContent>
        <mimeMap fileExtension=".geojson" mimeType="application/geojson" />
      </staticContent>
    </system.webServer>
    

    使用名为“Data”的文件夹中的文件。

    d3.json("/Data/geo/states.geojson", function (error, geodata) {
    

    ... }

    我看到使用“App\u Data”文件夹时出错。

    d3.json("/App_Data/geo/states.geojson", function (error, geodata) {