谷歌地图API显示空白地图

Google Maps API showing blank map

本文关键字:地图 空白 显示 API 谷歌地图      更新时间:2023-09-26

我确信这是一个基本问题,但我现在已经撞到墙上太多次了,所以希望有人会同情我!

我有下面的例子,但它所做的只是显示一个灰色的框,根本没有地图。有人能告诉我为什么吗?

我已经检查过我是否真的返回了一个结果,它似乎运行得很好。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title></title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <style>
            html, body, #map-canvas {margin: 0;padding: 0;height: 100%;}
        </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
      var geocoder;
      var map;
      function initialize() 
      {
        geocoder = new google.maps.Geocoder();        
        geocoder.geocode( { 'address': "England"}, function(results, status) 
        {
          if (status == google.maps.GeocoderStatus.OK) 
          {
            var mapOptions = {
              zoom: 8,
              center: new google.maps.LatLng(results[0].geometry.location),
              mapTypeId: google.maps.MapTypeId.ROADMAP
            }
            // Let's draw the map
            map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
          } 
          else 
          {
            alert("Geocode was not successful for the following reason: " + status);
          }
        });
      }
    initialize();
    </script>
  </head>
  <body onload="">
 <div id="map-canvas" style="width: 320px; height: 480px;"></div>
</body>
</html>    

尝试调整浏览器窗口的大小,用光标摇一摇浏览器/从浏览器选项卡拖动它,就会看到地图出现。

由于MVC部分视图中的一些奇怪原因,谷歌地图为空,您的地图正在工作,只需要调整大小。

用光标摇动浏览器窗口听起来很有趣,但它确实有效,我不知道如何最好地描述它

谢谢,Anurag

=================================================

我的最终工作代码如下:

`

<script type="text/javascript">
    $(document).ready(function () {
        (function () {
            var options = {
                zoom: 6,
                center: new google.maps.LatLng(-2.633333, 37.233334),
                mapTypeId: google.maps.MapTypeId.TERRAIN,
                mapTypeControl: false
            };
            // init map
            var map = new google.maps.Map(document.getElementById('map_canvas'), options);

            var arrLocation = [];
            $("#markerDiv").find("div").each(function () {
                var Lat = $(this).find("input[id='Latitude']").val();
                var Lon = $(this).find("input[id='Longitude']").val();
                var Id = $(this).find("input[id='Id']").val();
                var AssessmentDet = $(this).find("input[id='AssessmentDateTime']").val();
                var LocAcc = $(this).find("input[id='LocationAccuracy']").val();
                var assessorName = $(this).find("input[id='AssessorName']").val();
                var partnerName = $(this).find("input[id='PartnerName']").val();
                arrLocation.push({
                    Id: Id,
                    Latitude: Lat,
                    Longitude: Lon,
                    AssessmentDate: AssessmentDet,
                    LocationAccuracy: LocAcc,
                    AssessorDetail: assessorName,
                    PartnerName: partnerName
                    });
            });
            var allMarkers = [];
            for (var i = 0; i < arrLocation.length; i++) {
                //final position for marker, could be updated if another marker already exists in same position
                var latlng = new google.maps.LatLng(arrLocation[i].Latitude, arrLocation[i].Longitude);
                var finalLatLng = latlng;
                var comparelatlng = "(" + arrLocation[i].Latitude + "," + arrLocation[i].Longitude + ")";
                var copyMarker = arrLocation[i];
                var marker = new google.maps.Marker({
                    position: new google.maps.LatLng(arrLocation[i].Latitude, arrLocation[i].Longitude),
                    map: map,
                    title: 'Equine # ' + arrLocation[i].Id,
                    icon:"abc.png"
                });

                var markerInfo = "Reference # : <b>" + arrLocation[i].Id + "</b><br/>";
                markerInfo = markerInfo + "Assessor : <b>" + arrLocation[i].AssessorDetail + "</b><br/>";
                markerInfo = markerInfo + "Date :  <b>" + arrLocation[i].AssessmentDate + "</b><br/>";
                markerInfo = markerInfo + "Partner :  <b>" + arrLocation[i].PartnerName + "</b>";(function (marker, i) {
  bindInfoWindow(marker, map, new google.maps.InfoWindow(), markerInfo);
                })(marker, i);
            }
        })();
    });
    function bindInfoWindow(marker, map, infowindow, html) {
        google.maps.event.addListener(marker, 'click', function () {
            infowindow.setContent(html);
            infowindow.open(map, marker);
        });
    }

</script>

`

results[0].geometry.location已经是一个latLng对象,所以您可以说:

center: results[0].geometry.location

在这里找到工作小提琴:http://jsfiddle.net/87z9K/

这是因为提供了"google.maps.LatLng"。为coords提供一个测试,它就会起作用。更换线路

center: new google.maps.LatLng(results[0].geometry.location),

带有

center: new google.maps.LatLng(-34.397, 150.644)

获取英格兰坐标

这并不是你的问题,而是密切相关的。

我发现我必须用有效的centre设置mapOptions,就像这样:

new google.maps.Map(mapCanvas, {
    center: new google.maps.LatLng(-34.397, 150.644)
});

如果我没有输入贴图选项,或者如果我输入了,但它没有有效的center集,我会得到一个没有加载磁贴的空白贴图。

如果贴图的高度/宽度为0,也可能发生这种情况。

我试图设置地图的MapTypeId,正如Anurag所建议的那样:

map.setMapTypeId(google.maps.MapTypeId.TERRAIN);

我可以看到您的代码存在一个常见的javascript问题。

您的脚本可能试图在加载HTML之前将地图嵌入到页面中。

这样调用函数(还有其他方法)。

<body onload="initialize()">