谷歌地图v3:标记和折线无法渲染。我的代码出了什么问题?

时间:2015-12-10 13:00:19

标签: javascript google-maps-api-3 google-maps-markers google-polyline

问题:我正在尝试使用Google地图v3将标记和折线添加到我的地图中,但它不起作用。它实际上没有渲染任何东西,我一直在关注谷歌文档,我有一个有效的API密钥所以不知道...建议?先感谢您!

<!DOCTYPE html>
    <html>
      <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <title>Simple Polylines</title>
     <style>
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
        <script>

    function initMap() {
    var myOptions = {
      zoom: 4,
      center: new google.maps.LatLng(42.0, 10.0),
      mapTypeId: google.maps.MapTypeId.TERRAIN
    }

    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var points = [     
        new google.maps.LatLng(39.0, -3.0),     
        new google.maps.LatLng(52.1, 12.1),     
        new google.maps.LatLng(40.2, 32.7)  
    ]; 
    var markers = [];
    var path = [];
    for (var i = 0; i < points.length; ++i) {
        var marker = new google.maps.Marker({map: map, position: points[i]});
        markers.push(marker);
        path.push(marker.position);
    }
    var polyline = new google.maps.Polyline({     
        path: path,     
        strokeColor: "#FF0000"
    });    
    polyline.setMap(map);  
    }

    </script>

    <script async defer
        src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD6SfOqSpJQa45i0LWO-9zSrV9HDPzACYg&signed_in=true&callback=initMap"></script>
   
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

正如Dr.Molle在评论中所说:地图容器的ID是map,而不是map_canvas

&#13;
&#13;
    function initMap() {
    var myOptions = {
      zoom: 4,
      center: new google.maps.LatLng(42.0, 10.0),
      mapTypeId: google.maps.MapTypeId.TERRAIN
    }

    var map = new google.maps.Map(document.getElementById("map"), myOptions);
    var points = [     
        new google.maps.LatLng(39.0, -3.0),     
        new google.maps.LatLng(52.1, 12.1),     
        new google.maps.LatLng(40.2, 32.7)  
    ]; 
    var markers = [];
    var path = [];
    for (var i = 0; i < points.length; ++i) {
        var marker = new google.maps.Marker({map: map, position: points[i]});
        markers.push(marker);
        path.push(marker.position);
    }
    var polyline = new google.maps.Polyline({     
        path: path,     
        strokeColor: "#FF0000"
    });    
    polyline.setMap(map);  
    }
&#13;
html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #map {
        height: 100%;
      }
&#13;
    <div id="map"></div>
    <script async defer
        src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap"></script>
&#13;
&#13;
&#13;