Google maps Google Map API v3示例在他们的网站上没有';如果没有API密钥,则无法工作,但可以使用密钥。我做错了什么?

Google maps Google Map API v3示例在他们的网站上没有';如果没有API密钥,则无法工作,但可以使用密钥。我做错了什么?,google-maps,google-maps-api-3,polyline,google-polyline,Google Maps,Google Maps Api 3,Polyline,Google Polyline,问题:我正在尝试使用一个没有API键的Google Map API v3示例,但它不会呈现。(如果我使用钥匙,它会渲染)。有人知道为什么吗 我决定用这个例子 () 为了在没有钥匙的情况下工作,我替换了以下脚本: <script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap"></script>

问题:我正在尝试使用一个没有API键的Google Map API v3示例,但它不会呈现。(如果我使用钥匙,它会渲染)。有人知道为什么吗

我决定用这个例子 ()

为了在没有钥匙的情况下工作,我替换了以下脚本:

<script async defer src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=initMap"></script>

用另一个脚本

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>

代码如下:


简单多段线
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图{
身高:100%;
}
//此示例创建一条2像素宽的红色多段线,显示William的路径
//Kingsford Smith在加利福尼亚州奥克兰和加利福尼亚州之间的首次跨太平洋航班
//澳大利亚布里斯班。
函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
缩放:3,
中心:{lat:0,lng:-180},
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var FlightPlan坐标=[
{lat:37.772,lng:-122.214},
{lat:21.291,lng:-157.821},
{拉丁美洲:-18.142,液化天然气:178.431},
{拉丁美洲:-27.467,液化天然气:153.027}
];
var flightPath=new google.maps.Polyline({
路径:FlightPlan坐标,
测地线:正确,
strokeColor:“#FF0000”,
笔划不透明度:1.0,
冲程重量:2
});
flightPath.setMap(map);
}

将谷歌地图脚本放入头部。在你调用它之前必须包含它

调用initMap函数来运行您设置的函数。只定义了函数initMap(),但在此之前您尚未运行它

<!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>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body>
    <div id="map"></div>
    <script>

// This example creates a 2-pixel-wide red polyline showing the path of William
// Kingsford Smith's first trans-Pacific flight between Oakland, CA, and
// Brisbane, Australia.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 3,
    center: {lat: 0, lng: -180},
    mapTypeId: google.maps.MapTypeId.TERRAIN
  });

  var flightPlanCoordinates = [
    {lat: 37.772, lng: -122.214},
    {lat: 21.291, lng: -157.821},
    {lat: -18.142, lng: 178.431},
    {lat: -27.467, lng: 153.027}
  ];
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  flightPath.setMap(map);
}

initMap();
    </script>
</body>
</html>

简单多段线
html,正文{
身高:100%;
保证金:0;
填充:0;
}
#地图{
身高:100%;
}
//此示例创建一条2像素宽的红色多段线,显示William的路径
//Kingsford Smith在加利福尼亚州奥克兰和加利福尼亚州之间的首次跨太平洋航班
//澳大利亚布里斯班。
函数initMap(){
var map=new google.maps.map(document.getElementById('map'){
缩放:3,
中心:{lat:0,lng:-180},
mapTypeId:google.maps.mapTypeId.TERRAIN
});
var FlightPlan坐标=[
{lat:37.772,lng:-122.214},
{lat:21.291,lng:-157.821},
{拉丁美洲:-18.142,液化天然气:178.431},
{拉丁美洲:-27.467,液化天然气:153.027}
];
var flightPath=new google.maps.Polyline({
路径:FlightPlan坐标,
测地线:正确,
strokeColor:“#FF0000”,
笔划不透明度:1.0,
冲程重量:2
});
flightPath.setMap(map);
}
initMap();

请提供一个例子来说明这个问题。除了一个更充实的例子外,您是否遇到了特定的js错误?@Locke125没有特定的错误。这肯定是关于google API键的脚本,因为当我用maps API键替换该位时,它工作了,您真的从API include中删除了
callback=initMap
?这将阻止调用
initMap
函数,您需要添加代码以在页面加载时调用它。非常感谢!你太棒了:D