嵌入在C#WebBrowser控件中的Google地图不显示缩放/卫星按钮

时间:2018-05-24 12:54:04

标签: c# google-maps

我在Google.Windows.Forms.WebBrowser控件中嵌入了Google Maps。直到最近它一切正常。但现在不再显示地图类型按钮(路线图,卫星等)和缩放按钮:

enter image description here

直接在IE11中打开相同的HTML文件可以顺利运行(here)。

我这样做是为了强制嵌入式IE不使用兼容模式:

  • HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION2af8
  • 中为应用程序设置IE仿真模式
  • 在HTML的<meta http-equiv="X-UA-Compatible" content="IE=Edge" />中使用<head>

我正在使用.NET Framework 4.5.2。这里有一个重现问题的小例子项目:https://github.com/nharrer/gmap-dotnet-example

以下是HTML的一部分:

<!DOCTYPE html>
<html>

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

    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Testpage</title>

    ....
</head>

<body>
    <div id="map"></div>
    <script>
        function initMap() {
            map = new google.maps.Map(document.getElementById('map'), {
                zoom: 4,
                center: { lat: 48.2081743, lng: 16.37381890000006 }
            });

            var marker = new google.maps.Marker({
                position: { lat: 48.2081743, lng: 16.37381890000006 },
                map: map,
                title: 'Mark1!'
            });

            var marker2 = new google.maps.Marker({
                position: { lat: 50.2081743, lng: 12.37381890000006 },
                map: map,
                title: 'Mark2!'
            });
        }
    </script>

    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAR1yYbZk62bSF0-QWNfVm5FWE_Jpv-ExA&callback=initMap"></script>
</body>

</html>

以下是设置兼容性注册表值并初始化WebBrowser控件的c#代码:

public TestForm()
        {
            FixBrowserEmulation();

            InitializeComponent();

            bool designTime = LicenseManager.UsageMode == LicenseUsageMode.Designtime;
            if (!designTime) {
                mapBrowser.ScriptErrorsSuppressed = false;

                string docFile = Path.Combine(Application.StartupPath, "maptest.html");
                string documentText = File.ReadAllText(docFile);
                mapBrowser.DocumentText = documentText;
            }
        }

        // see: 
        // https://stackoverflow.com/questions/17922308/use-latest-version-of-internet-explorer-in-the-webbrowser-control
        // https://blog.malwarebytes.com/101/2016/01/a-brief-guide-to-feature_browser_emulation/
        private static void FixBrowserEmulation()
        {
            var appName = Process.GetCurrentProcess().ProcessName + ".exe";

            // 11000 (0x2AF8) - Internet Explorer 11. Webpages containing standards-based !DOCTYPE directives are displayed 
            // in IE11 edge mode. Default value for IE11.
            int? mode = 0x2AF8;

            try {
                const string regpath = @"SOFTWARE\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION";
                using (RegistryKey regkey = Registry.CurrentUser.CreateSubKey(regpath)) {
                    if (regkey == null) {
                        Debug.WriteLine("Error: Can not access: " + @"HKEY_CURRENT_USER\\" + regpath);
                        return;
                    }

                    var currentMode = regkey.GetValue(appName) as int?;
                    if (currentMode == mode) {
                        Debug.WriteLine("Info: FEATURE_BROWSER_EMULATION is correct.");
                        return;
                    }

                    regkey.SetValue(appName, mode, RegistryValueKind.DWord);

                    currentMode = regkey.GetValue(appName) as int?;
                    if (currentMode == mode) {
                        Debug.WriteLine("Info: FEATURE_BROWSER_EMULATION set to " + currentMode);
                    } else {
                        Debug.WriteLine("Info: FEATURE_BROWSER_EMULATION modification failed. Current value: " + currentMode);
                    }
                }
            } catch (Exception ex) {
                Debug.WriteLine(ex.Message);
            }
        }

1 个答案:

答案 0 :(得分:2)

可能为时已晚,但是如果有人偶然发现了缩放和街景控件消失的上述问题,请确保将AllowNavigation控件的WebBrowser属性设置为True。所以在这种情况下,您应该

mapBrowser.AllowNavigation = true;