[visual-studio].Net windows 应用程序中的浏览器 / Google Maps API v3

发布时间: 2014/3/1 2:12:38
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我正在开发一个应用程序中,用户将能够输入所需的地址,然后按按钮。此地址将会发现在上 WebBrowser .net windows 应用程序中的控件。我知道你可以上运行 javascript WebBrowser 通过使用 WebBrowser1.DocumentText ,和调用由脚本 WebBrowser1.Document.InvokeScript ......

我有这样的小问题,我想知道是否有人可以教我正确的方式来做。

代码:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

  Dim AddressMap As String

  AddressMap = AddressM.Text

  WebBrowser1.DocumentText = "<html><head><meta name='viewport' content='initial-scale=1.0, user-scalable=no' />" & _
    "<script type='text/javascript' src='http://maps.google.com.mx/maps/api/js?sensor=true&language;=es'></script>" & _
    "<script type='text/javascript'>" & _
    "var geocoder; var map;" & _
    "function initialize() " & _
    "{geocoder = new google.maps.Geocoder(); var myOptions = { zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP()" & _
    "} var(address = " & AddressMap & ")" & _
    "geocoder.geocode({ 'address': address }, function (results, status) {" & _
                    "if (status == google.maps.GeocoderStatus.OK) {" & _
                        "map.setCenter(results[0].geometry.location);" & _
                        "var marker = new google.maps.Marker({" & _
                           " map: map," & _
                            "position: results[0].geometry.location });" & _
                    "} else {" & _
                    "}});" & _
     "map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);}" & _
     "</script></head><div id='map_canvas' style='width:100%; height:100%'></div></body></html>"

   WebBrowser1.Document.InvokeScript("Initialize")

End Sub

下面是另一段代码,我认为应该工作,但仍然出现脚本错误"错误发生在此页上的脚本"线 1 CHar 124 错误预期;代码 0 URL about.blank

 WebBrowser1.DocumentText = "<html><head><script type='text/javascript' src='http://maps.google.com.mx/maps/api/js?sensor=false&language;=es'></script> " +
                           "<script type='text/javascript'> " +
                           "var geocoder; " +
                           "var map; " +
                           "function initialize(address) { " +
                           "geocoder = new google.maps.Geocoder(); " +
                           "var myOptions = { zoom: 16 } " +
                           "geocoder.geocode({ 'address': address }, function (results, status) { map.setCenter(results[0].geometry.location); " +
                           "var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); " +
                           "map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); " +
                           "} " +
                           "</script> " +
                           "</head> " +
                           "<body> <div id='map_canvas' style='width:100%; height:100%'></div> </body> </html>"

WebBrowser1.Document.InvokeScript("initialize", New String() {AddressM.Text})

我非常感谢帮助

利奥体育

这是另一段代码略有不同前两项,它包括和 else 语句从实际运行谷歌 html 拔出地图脚本。

WebBrowser1.DocumentText = "<html><head><script type='text/javascript' src='http://maps.google.com.mx/maps/api/js?sensor=false&language;=es'></script> " +
                       "<script type='text/javascript'> " +
                       "var geocoder; " +
                       "var map; " +
                       "function initialize() { " +
                       "geocoder = new google.maps.Geocoder(); " +
                       "var myOptions = { zoom: 16, mapTypeId: google.maps.MapTypeId.ROADMAP } " +
                       "var(address = 'Miami Beach, Flordia') " +
                       "geocoder.geocode({ 'address': address }, " +
                       "function (results, status) { " +
                       "if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); " +
                       "var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); " +
                       "} else { alert('Geocode was not successful !);}}); " +
                       "map = new google.maps.Map(document.getElementById('map_canvas'), myOptions); } " +
                       "</script> " +
                       "</head> " +
                       "<body> <div id='map_canvas' style='width:100%; height:100%'></div> </body> </html>"

        WebBrowser1.Document.InvokeScript("initialize")

下面的 html 代码

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

<script type="text/javascript" src="http://maps.google.com.mx/maps/api/js?sensor=true&language=es"></script>
<script type="text/javascript">

    var geocoder;
    var map;


    function initialize() {

        geocoder = new google.maps.Geocoder();

        var myOptions = {
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        //var address = document.getElementById("address").value;
        var address = "Miami Beach, Flordia" //change the address in order to search the google maps

        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });

        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }


    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>

  </body>
</html>

解决方法 1:

把解决方案

HTML

在 C:\page.html 和使用此代码创建页的 html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />

<script type="text/javascript" src="http://maps.google.com.mx/maps/api/js?sensor=true&language=es"></script>
<script type="text/javascript">

    var geocoder;
    var map;


    function initialize(address) {

        geocoder = new google.maps.Geocoder();

        var myOptions = {
            zoom: 16,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        geocoder.geocode({ 'address': (address ? address : "Miami Beach, Flordia")}, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location
                });
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });

        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    }


    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>

  </body>
</html>

Windows 窗体-C#

private void Form1_Load(object sender, EventArgs e)
{
    // Put uri (http://www.example.com/page.html and c:\page.html is valid address)
    webBrowser1.Url = new Uri(@"C:\page.html");
}

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Document.InvokeScript("initialize", 
            new string[] { textBox1.Text });
}

和需要与此组件具有此名称的形式: textBox1button1webBrowser1

赞助商