繁体   English   中英

显示空白地图的 Google Maps API

[英]Google Maps API showing blank map

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

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

我已经检查过我实际上返回了一个结果,它似乎工作正常。

<!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 部分视图谷歌地图中的一些奇怪原因,谷歌地图显示为空白,您的地图正在工作,只需调整大小即可。

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

谢谢,阿努拉格

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

我的最终工作代码如下:

`

<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”。 提供测试坐标,它将起作用。 更换线

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()">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM