简体   繁体   English

如何将kml文件写入可公开访问的服务器?

[英]How to write a kml file to publicly accessible server?

I want to render a KML file in google maps from my asp.net MVC 3 app. 我想从我的asp.net MVC 3应用程序在Google地图中呈现KML文件。

From Google Maps JavaScript API V3, I'm using KmlLayer('url') to render the kml in google maps. 从Google Maps JavaScript API V3,我正在使用KmlLayer('url')在Google地图中呈现kml。 I have read that kml file needs to be in a publicly accessible server. 我读过kml文件必须位于可公开访问的服务器中。 I could render the kml file locally with third party javascripts but they can be prone to errors. 我可以使用第三方javascript在本地渲染kml文件,但它们容易出错。 ( Loading a local .kml file using google maps? ) (是否要使用Google地图加载本地.kml文件?

The kml files I want to render are stored in a SQL server database as byte arrays. 我要呈现的kml文件以字节数组形式存储在SQL Server数据库中。 So for one Kml file I have to write the byte array into a path with kml extension. 因此,对于一个Kml文件,我必须将字节数组写入具有kml扩展名的路径中。 I have done this with File.WriteAllBytes(path, byte), where path = local path and byte = byte array from Database. 我已经用File.WriteAllBytes(path,byte)完成了此操作,其中path =数据库的本地路径和byte =字节数组。 THis is done in the Controller of MVC app. 这是在MVC应用的控制器中完成的。 This is the code: 这是代码:

public ActionResult MapView(int id)
        {
          Incident inc = db.Incidents.Find(id);
            if (inc.IncidentDatas.Count != 0)
            {
                ViewBag.ListKmlUrls = kmlFileStore(inc);
            }

            return View(inc);
        }

public List<string> kmlFileStore(Incident inc)
        {
            List<string> listKmlUrls = new List<string>();
            // Specify a "currently active folder"
            string activeDir = @"c:\incident" + inc.incId + @"\incidentData";

            //Create a new subfolder under the current active folder
            string newPath = System.IO.Path.Combine(activeDir, "kmlFiles");

            // Create the subfolder
            System.IO.Directory.CreateDirectory(newPath);

            String url;
            foreach(var d in inc.IncidentDatas) {
                url = @"c:\incident" + inc.incId + @"\incidentData\kmlFiles\" + d.id + ".kml";

                 //dataContent is byte[]
                System.IO.File.WriteAllBytes(url, d.dataContent);
                listKmlUrls.Add(url);
            }

            return listKmlUrls;
        }

The idea is the view will access the list of urls through viewbag to pass urls to javascript method KmlLayer(...). 这个想法是视图将通过viewbag访问URL列表,以将URL传递给javascript方法KmlLayer(...)。 But the urls here are only local paths. 但是这里的URL只是本地路径。

So how can the MVC app store the kml file to a publicly accessible server so that it can pass a url to KmlLayer(...)? 那么,MVC应用程序如何将kml文件存储到可公开访问的服务器,以便它可以将URL传递给KmlLayer(...)? This would have to be done programmatically. 这必须以编程方式完成。

I'm currently accessing my MVC app and database from localhost. 我目前正在从本地主机访问我的MVC应用程序和数据库。 I have a static Ip and name. 我有一个静态的IP和名称。 I would also like to publish the app and database for online access. 我还想发布该应用程序和数据库以进行在线访问。 Not sure how to proceed, please give me some advice/guidance. 不确定如何进行,请给我一些建议/指导。 Thanks. 谢谢。

There are some problem to make kml file publicly accessible. 使kml文件可公开访问存在一些问题。 Say the file location must be accessible by google. 假设文件位置必须可由Google访问。

If you want to embed Google Earth in website then there are three methods of importing KML into the plugin. 如果您想将Google Earth嵌入网站,则可以通过以下三种方法将KML导入插件。 1. KmlNetworkLink 2. fetchKml 3. ParseKml 1. KmlNetworkLink 2. fetchKml 3. ParseKml

Both 1 & 2 need kml file stored in server that must publicly accessible but 3. ParseKml works better way. 1和2都需要存储在服务器中的kml文件,该文件必须可以公共访问,但3. ParseKml的工作方式更好。

<head>
    <title>parsekml_example.html</title>
    <script src="//www.google.com/jsapi?key=ABQIAAAA5El50zA4PeDTEMlv-sXFfRSsTL4WIgxhMZ0ZK_kHjwHeQuOD4xTdBhxbkZWuzyYTVeclkwYHpb17ZQ"></script>
    <script type="text/javascript">
        var ge;
        var placemark;
        var object;

        google.load("earth", "1");

        function init() {
            google.earth.createInstance('map3d', initCB, failureCB);
        }

        function initCB(instance) {
            ge = instance;
            ge.getWindow().setVisibility(true);
            var kmlString = ''
                         + '<?xml version="1.0" encoding="UTF-8"?>'
                         + '<kml xmlns="http://www.opengis.net/kml/2.2">'

                         + '<Document>'
                         + '  <Camera>'
                         + '    <longitude>-122.444633</longitude>'
                         + '    <latitude>37.801899</latitude>'
                         + '    <altitude>139.629438</altitude>'
                         + '    <heading>-70.0</heading>'
                         + '    <tilt>75</tilt>'
                         + '  </Camera>'

                         + '  <Placemark>'
                         + '    <name>Placemark from KML string</name>'
                         + '    <Point>'
                         + '      <coordinates>-122.448425,37.802907,0</coordinates>'
                         + '    </Point>'
                         + '  </Placemark>'

                         + '</Document>'
                         + '</kml>';

            var kmlObject = ge.parseKml(kmlString);
            ge.getFeatures().appendChild(kmlObject);
            ge.getView().setAbstractView(kmlObject.getAbstractView());
        }

        function failureCB(errorCode) {
        }

        google.setOnLoadCallback(init);
    </script>  
</head>
<body>
    <div id="map3d" style="height: 400px; width: 600px;">

    </div>   
</body>

For more detail see http://code.google.com/apis/earth/documentation/kml.html 有关更多详细信息,请参见http://code.google.com/apis/earth/documentation/kml.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM