c# google maps api - javascript errors on the html part

匿名 (未验证) 提交于 2019-12-03 09:18:39

问题:

I newly want to use the googlemap api to display some markers on a map in a simple C# windows form with visual studio.

I use a "web browser" component to display a generated html file with the basic html code from google plus customized coordinate.

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

This code is working good, but my application text me that java script generate errors.

Can you give me some help, i don't understand why there is this error and finding topics or code exemple is hard.

Thanks you for reading me.

Error

回答1:

I understand your application uses Web Browser control that emulates a certain version of IE.

Please note that the current release version of Maps JavaScript API doesn't support neither old IE versions nor compatibility mode. You should use supported browsers as mentioned in the document:

https://developers.google.com/maps/documentation/javascript/browsersupport

If you are working with WebBrowser controls, it can default to an IE 7 rendering mode: https://weblog.west-wind.com/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version

As you can see from the above article, you can write something in the registry to force the control to a newer IE version. It is recommended to specify at least version 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

Additionally, you can add the meta tags like

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE10" />

or

<meta http-equiv="X-UA-Compatible" content="IE=edge">

in your html page header section in order to force rendering mode for the modern version of IE.

As an alternative solution you can consider a different web browser embedded control. For example, you can look at Chromium Embedded Framework.

https://en.wikipedia.org/wiki/Chromium_Embedded_Framework

There is a useful discussion in the public issue tracker as well:

https://code.google.com/p/gmaps-api-issues/issues/detail?id=9004



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!