[visual-studio].NET windows 应用程序 / 谷歌地图 api,不能两次搜索相同的地址

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

一段时间后有人从计算器帮了我一个漂亮的解决方案要带到使用谷歌地图 api 上的 windows 应用程序

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 倍我得到错误的脚本

错误消息

这个问题如何能解决?

谢谢你在前进

利奥体育

解决方法 1:

地理编码是异步的所以在某些情况下 (具体取决于如何快速返回),您的地图尚未创建当您尝试使用它。您需要创建地图第一次和地理代码 afterwrds。相反试试这个:

    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>
官方微信
官方QQ群
31647020