我最近想使用 googlemap api 通过 Visual Studio 在简单的 C# Windows 窗体中在 map 上显示一些标记。
我使用“网络浏览器”组件来显示生成的 html 文件,其中包含来自 google 的基本 html 代码以及自定义坐标。
const string htmlPath = "D:/map.html";
StreamWriter sw = new StreamWriter(htmlPath, false, System.Text.Encoding.GetEncoding(437));
string centerLongitude = centerLongitudeTextBox.Text;
string centerLatitude = centerLatitudeTextBox.Text;
sw.WriteLine("<!DOCTYPE html>");
sw.WriteLine("<html>");
sw.WriteLine("<head>");
sw.WriteLine("<meta charset=\"utf-8\">");
sw.WriteLine("<style>");
sw.WriteLine("html, body, #map{");
sw.WriteLine("margin :0;");
sw.WriteLine("padding: 0;");
sw.WriteLine("height: 100%");
sw.WriteLine("}");
sw.WriteLine("</style>");
//sw.WriteLine("<link rel=\"stylesheet\" href=\"/maps/documentation/javascript/demos/demos.css\">");
sw.WriteLine("</head>");
sw.WriteLine("<body>");
sw.WriteLine("<div id=\"map\"></div>");
sw.WriteLine("<script>");
sw.WriteLine("function initMap() {");
sw.WriteLine("// Create a map object and specify the DOM element for display.");
sw.WriteLine("var map = new google.maps.Map(document.getElementById('map'), {");
sw.WriteLine("center: { lat: "+ centerLatitude +", lng: "+ centerLongitude +"},");
sw.WriteLine("scrollwheel: false,");
sw.WriteLine("zoom: 8");
sw.WriteLine("});");
sw.WriteLine("}");
sw.WriteLine("</script>");
sw.WriteLine("<script src=\"https://maps.googleapis.com/maps/api/js?key=MYKEY of course&callback=initMap\"");
sw.WriteLine("async defer></script>");
sw.WriteLine("</body>");
sw.WriteLine("</html>");
sw.Close();
webBrowser1.Navigate("file:///" + htmlPath);
这段代码运行良好,但我的应用程序发短信给我说 java 脚本生成错误。
你能给我一些帮助吗,我不明白为什么会出现这个错误,并且很难找到主题或代码示例。
感谢您阅读我的文章。
最佳答案
我了解您的应用程序使用模拟特定版本 IE 的 Web 浏览器控件。
请注意,当前发布的 Maps JavaScript API 版本既不支持旧版 IE,也不支持兼容模式。您应该使用文档中提到的支持的浏览器:
https://developers.google.com/maps/documentation/javascript/browsersupport
如果您使用 WebBrowser 控件,它可以默认为 IE 7 呈现模式: https://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version
从上面的文章中可以看出,您可以在注册表中编写一些内容来强制控制较新的 IE 版本。建议至少指定版本 10。
http://www.codeproject.com/Articles/793687/Configuring-the-emulation-mode-of-an-Internet-Expl Use latest version of Internet Explorer in the webbrowser control
此外,您可以添加元标记,例如
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE10" />
或
<meta http-equiv="X-UA-Compatible" content="IE=edge">
在您的 html 页面标题部分中,以强制现代版本 IE 的呈现模式。
作为替代解决方案,您可以考虑使用不同的 Web 浏览器嵌入式控件。例如,您可以查看 Chromium Embedded Framework。
https://en.wikipedia.org/wiki/Chromium_Embedded_Framework
公共(public)问题跟踪器中也有一个有用的讨论:
https://code.google.com/p/gmaps-api-issues/issues/detail?id=9004
关于javascript - C# google map api - html 部分的 javascript 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40486895/