我是JavaScript新手 我已经阅读了这个问题:Google Maps JS API v3 - Simple Multiple Marker Example并尝试了Daniel Vassallo提供的答案
var Australia = new google.maps.LatLng(-27.16881, 132.72259);
var placesIhaveBeen = [
['Aalborg Airport Flight leaves at:xxxxxx', 57.04807, 9.91857]
['Copenhagen Airport Arrives leaves at:xxxxxx', 55.61541, 12.64885]
['Dubai Airport Flight Arrives at: xxxxx', 25.25278, 55.36444]
['Sydney Airport Flight Arrives at:xxxxxx', -33.93947, 151.17522]];
var markers = [];
var iterator = 0;
var map;
var infowindows = new google.maps.InfoWindow();
var marker, i;
function initialize()
{
var mapOptions = {
zoom: 4,
center: Australia,
mapTypeControl: false
};
map = new google.maps.Map(document.getElementById("googleMap-Canvas"),
mapOptions);
//flight path Polyline
var flightPlanCoordinates = [
new google.maps.LatLng(57.04807, 9.91857),
new google.maps.LatLng(55.61541, 12.64885),
new google.maps.LatLng(25.25278, 55.36444),
new google.maps.LatLng(-33.93947, 151.17522)
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
//end of flight path polyline
for (i = 0; i < placesIhaveBeen.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(placesIhaveBeen[i][1], placesIhaveBeen[i][2], placesIhaveBeen[i][3], placesIhaveBeen[i][4]),
map: map
});
google.maps.eve.addDomListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(placesIhaveBeen[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
}
google.maps.event.addDomListener(window, 'load', initialize);
那是我的整个剧本:S
我知道我的中间有一些飞行路径,我希望它也能显示 - 作为标记(placesIhaveBeen)。
所以这笔交易是我希望标记与infowindows和折线同时显示,但我不能让它工作 - 请帮助:)
答案 0 :(得分:1)
您有几个错误:
placesIhaveBeen
的数组定义中缺少逗号:
var placesIhaveBeen = [
['Aalborg Airport Flight leaves at:xxxxxx', 57.04807, 9.91857],
['Copenhagen Airport Arrives leaves at:xxxxxx', 55.61541, 12.64885],
['Dubai Airport Flight Arrives at: xxxxx', 25.25278, 55.36444],
['Sydney Airport Flight Arrives at:xxxxxx', -33.93947, 151.17522]
];
infowindow
未定义,因此行
var infowindows = new google.maps.InfoWindow();
应该是
var infowindow = new google.maps.InfoWindow();
google.maps.eve.addDomListener
中有一个拼写错误。它应该是google.maps.event.addDomListener
。
见example at jsbin。地图包含的名称不同。