Google地图未正确加载到网络浏览器控件中

时间:2015-10-15 09:45:14

标签: c# windows winforms google-maps-api-3 webbrowser-control

我有一个像Map.Html这样的html文件

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
    <title></title>
    <script type="text/javascript" src="http://maps.google.com.mx/maps/api/js?sensor=true"></script>
    <script type="text/javascript">

        var geocoder;
        var map;

        function initialize() {

            geocoder = new google.maps.Geocoder();

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

            var address = "Ahmedabad, India" //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
                    });

                    var infoWindow = new google.maps.InfoWindow({
                        content: 'Hello'
                    });

                    google.maps.event.addListener(marker, "click", function (e) {
                        infoWindow.open(map, marker);
                    });

                } 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>

然后我使用下面的代码在webbrowser控件DocumentText中加载这个html文件:

using (StreamReader reader = new StreamReader(System.Windows.Forms.Application.StartupPath + "\\Map.html"))
{
_mapHTML = reader.ReadToEnd();
}
webBrowser1.DocumentText = _mapHTML;

但地图未正确加载。 放大/缩小,地图/卫星选项,标记在地图显示上显示一个白色图层。

1 个答案:

答案 0 :(得分:1)

已解决 - 至少对我而言。

出于某种原因,Webbrowser控件默认为坏版本(实验?)。我更改了初始化脚本以指定当前的3.3版本,一切都恢复正常。

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.3"></script>