javascript - C# google map api - html 部分的 javascript 错误

标签 javascript c# visual-studio google-maps-api-3

我最近想使用 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 脚本生成错误。

你能给我一些帮助吗,我不明白为什么会出现这个错误,并且很难找到主题或代码示例。

感谢您阅读我的文章。

Error

最佳答案

我了解您的应用程序使用模拟特定版本 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/

相关文章:

javascript - 有没有办法为隔离范围别名提供默认值?

c# - 如何使用asp.net core从appsettings.json获取配置数据并使用react

c# - iTextSharp XMLWorker 解析真的很慢

javascript - 为什么我从 Express 服务器收到 "404 cannot POST"错误?

javascript - 我的无序列表是从 json 文件呈现的,但我希望每个列表元素周围都有一个边框

javascript - 在 C# 中创建一个 JavaScript 函数

c# - 从 .Net 中的 SAS 数据源读取数据

visual-studio - 一个解决方案上的多个启动项目的设置存储在哪里?

visual-studio - 为什么在构建安装项目时会得到 "An error occurred while validating. HRESULT = ' 8000400 5'"?

javascript - 同一页面中两个 jquery Accordion 的不同样式