我想從我的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和名稱。我也想發佈在線訪問應用程序和數據庫。不知道如何繼續,請給我一些建議/指導。謝謝。
好吧,但我要嵌入谷歌地圖來代替。 MVC應用程序需要顯示呈現kml文件的簡單路線圖。我不確定開放圖層是否具有本地渲染KmlLayer方法。隨着谷歌地球地圖的渲染速度較慢,也需要谷歌地球插件。在瀏覽器上訪問MVC應用程序的用戶可能沒有準備好插件... – Qwerty
我閱讀了有關使用Server.MapPath(「〜/ folder」)的內容。但不知道如何將kml文件存儲在Web服務器中並獲取存儲位置的URL ......我不希望KML存儲在訪問MVC應用程序的用戶系統中。 – Qwerty
它應該很簡單。根據您的地理信息,通過'System.IO.File.WriteAllText(path,contents)'創建kml文件。閱讀你可以試試這個'StreamReader file = new StreamReader(filePath); var kml = file.ReadToEnd();' – Shahdat