我试图将一个数组传递给函数,该函数接收这些值并将其绘制在地图上。
var coordinates = [
{lat: 37.772, lng: -122.214},
{lat: 21.291, lng: -157.821},
{lat: -18.142, lng: 178.431},
{lat: -27.467, lng: 153.027}
];
console.log(coordinates);
function initMap(coordinates) {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: 0, lng: -180},
mapTypeId: 'terrain'
});
var flightPlanCoordinates = coordinates;
console.log(flightPlanCoordinates);
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
但是由于某些原因,我得到了一个错误信息,说这不是一个数组。
这是一种情况,其中一个变量是 shadow
而根据经验法则,内部变量会在同名的顶层变量中产生阴影。
大致等于这种情况。
let a = 2;
function test(){
let a = 3;
// `a` will be 3 inside the function body; it will shadow previous `a`
// ...
}
function test2(a){
// also it can happen in function argument name declaration
// `a` here is parenthesis will shadow previous `a = 2`
// ...
}
所以在你的例子中,你可以将顶层变量重命名为... coordinates
或重命名函数参数 coordinate
到别的东西来解决影子。