0

不久前,stackoverflow 的某个人帮助我提供了一个在 Windows 应用程序上使用 google maps api 的不错的解决方案

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 应用程序代码

Public Class Form1

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Document.InvokeScript("initialize", New String() {AddressM.Text})

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        WebBrowser1.Url = New Uri("http://datasharesas.com/CompanyAdmin/map2.html")
    End Sub
End Class

项目图片

现在的问题是,当我查看同一个地址一次时,它工作得很好。当我搜索两次时,我得到一个灰屏,当我搜索超过 3 次时,我得到一个错误脚本

错误信息

这怎么能解决?

先感谢您,

里奥·P。

4

1 回答 1

1

地理编码是异步的,因此在某些情况下(取决于它返回的速度),当您尝试使用它时,您的地图尚未创建。您需要先创建地图,然后再进行地理编码。试试这个:

    script type="text/javascript">
    // Global vars
    // Sorry, I can't be bothered typing "google.maps." every time. ;-)
       var G = google.maps;
       var map;
       var geocoder = new G.Geocoder();

        function initialize() {
          createMap();
          geocode('Chicago');
        }

        function createMap() {
            var myOptions = {
                center: new G.LatLng(0,0),
                zoom: 4,
                mapTypeId: G.MapTypeId.ROADMAP
            }
            map = new G.Map(document.getElementById("map_canvas"), myOptions);
        }

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

        }


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

      </body>
    </html>
于 2012-09-01T06:03:14.597 回答