2012-01-24 36 views
0

我想從我的asp.net MVC 3應用程序呈現谷歌地圖中的KML文件。如何將kml文件寫入可公開訪問的服務器?

從谷歌地圖JavaScript API V3中,我使用KmlLayer('url')在谷歌地圖中呈現kml。 我已經讀過kml文件需要在公共可訪問的服務器中。我可以使用第三方javascript在本地渲染kml文件,但它們可能容易出錯。 (Loading a local .kml file using google maps?

我想渲染的kml文件作爲字節數組存儲在SQL服務器數據庫中。所以對於一個KML文件,我必須將字節數組寫入一個具有kml擴展名的路徑。我已經使用File.WriteAllBytes(path,byte)完成了這一操作,其中path = local path和byte = byte數組。這是在MVC應用程序的控制器中完成的。 這是代碼:

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

的想法是認爲將通過viewbag訪問的URL列表中的URL傳遞給JavaScript方法KmlLayer(...)。但這裏的網址只是本地路徑。

那麼MVC應用如何將kml文件存儲到可公開訪問的服務器上,以便它可以將URL傳遞給KmlLayer(...)?這將不得不以編程方式完成。

我目前正在從本地訪問我的MVC應用程序和數據庫。我有一個靜態IP和名稱。我也想發佈在線訪問應用程序和數據庫。不知道如何繼續,請給我一些建議/指導。謝謝。

回答

0

有一些問題可以公開訪問kml文件。說文件位置必須可以通過谷歌訪問。

如果你想在網站中嵌入谷歌地球,那麼有三種方法將KML導入到插件中。 1. KmlNetworkLink 2. fetchKml 3. parseKml會

存儲在必須公開訪問,但3 parseKml會工作更好的辦法服務器兩個1 & 2需要KML文件。

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

更多細節見http://code.google.com/apis/earth/documentation/kml.html

+0

好吧,但我要嵌入谷歌地圖來代替。 MVC應用程序需要顯示呈現kml文件的簡單路線圖。我不確定開放圖層是否具有本地渲染KmlLayer方法。隨着谷歌地球地圖的渲染速度較慢,也需要谷歌地球插件。在瀏覽器上訪問MVC應用程序的用戶可能沒有準備好插件... – Qwerty

+0

我閱讀了有關使用Server.MapPath(「〜/ folder」)的內容。但不知道如何將kml文件存儲在Web服務器中並獲取存儲位置的URL ......我不希望KML存儲在訪問MVC應用程序的用戶系統中。 – Qwerty

+0

它應該很簡單。根據您的地理信息,通過'System.IO.File.WriteAllText(path,contents)'創建kml文件。閱讀你可以試試這個'StreamReader file = new StreamReader(filePath); var kml = file.ReadToEnd();' – Shahdat