本文介绍了自动化谷歌地图在C#中的Web浏览器(问题JavaScript的执行正确)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不会使用API​​

我目前正在尝试使用网络浏览器在C#中加载谷歌地图,并自动专注于我的当前位置,但是,出于某种原因,我不能得到这个正常工作。这个想法很简单。加载谷歌地图,无论是和执行脚本专注于我的当前位置:

I am currently attempting to use a web browser in C# to load google maps and automatically focus on my current location, however, for some reason I cannot get this to work properly. The idea is simple. Load Google maps, and either execute the script to focus on my current location:

mapBrowser.Document.InvokeScript("mylocation.onButtonClick");

或者通过一个HTMLElement调用按钮,点击:

Or, invoke the button click through an HtmlElement:

HtmlElement myLocationButton = mapBrowser.Document.GetElementById("mylocation");
myLocationButton.InvokeMember("click");

但是,当然这些方法都不实际工作正常,返回的坐标是incorrent和地图从来没有真正的重点。我如何能妥善解决这个问题的任何想法?该脚本不被调用后才文档实际加载:

But, of course neither of these methods actually work correctly, the coordinates returned are incorrent and the map never actually focuses. Any ideas on how I can fix this issue properly? The scripts aren't invoked until after the document is actually loaded:

private void mapBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if(mapBrowser.Url.ToString() == "https://www.google.com/maps/preview/")
        {
            try
            {
                //HtmlElement myLocationButton = mapBrowser.Document.GetElementById("mylocation");
                //myLocationButton.InvokeMember("click");
                mapBrowser.Document.InvokeScript("mylocation.onButtonClick");
                //mapBrowser.Document.InvokeScript("focus:mylocation.main");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Invoking Script: " + ex.Message);
            }
        }
    }

所以我不认为是我的问题的原因。更令人沮丧的,如果我手动点击按钮自动对焦正常工作。

so I don't believe that is the cause of my problem. Even more frustratingly, the auto-focus works fine if I click the button manually.

任何帮助是AP preciated,谢谢!

Any help is appreciated, thank you!

(注意,你可能必须进入IE浏览器,并允许谷歌地图访问到你的位置,以复制这一问题正确)

(NOTE, you may have to go into IE and allow Google maps access to your location in order to replicate this issue properly)

推荐答案

我有几个问题倍,WebBrowser控件使用太旧版本的IE。您需要修改注册表以获取它使用IE的新版本。
我试图两手抓,IE 8和9,它给了我一个错误,但它在IE 10。

I've had problem few times that WebBrowser control uses too old version of IE. You need to modify registry to get it to use newer version of IE.I tried "https://www.google.com/maps/preview/" with both IE 8 and 9 and it gave me an error, but it works on IE 10.

请参阅:的

这篇关于自动化谷歌地图在C#中的Web浏览器(问题JavaScript的执行正确)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 02:13