Javascript 从一个数组中自动显示一个信息窗口

Javascript 从一个数组中自动显示一个信息窗口,javascript,google-maps,infowindow,Javascript,Google Maps,Infowindow,我有一个带有一系列指针和信息窗口的谷歌地图。我想在加载页面/地图时显示一个特定的信息窗口 我将如何做到这一点: 这是我的代码: function initialize() { var map; var bounds = new google.maps.LatLngBounds(); var mapOptions = { center: new google.maps.LatLng(53.47921, -1.00201), mapTypeId: google.maps.M

我有一个带有一系列指针和信息窗口的谷歌地图。我想在加载页面/地图时显示一个特定的信息窗口

我将如何做到这一点:

这是我的代码:

function initialize() {

var map;
var bounds = new google.maps.LatLngBounds();
var mapOptions = {
    center: new google.maps.LatLng(53.47921, -1.00201),
        mapTypeId: google.maps.MapTypeId.ROADMAP

};


map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
map.setTilt(45);

// Car Parks
var markers = [
    ['location 1', 53.47921, -1.00201],
    ['location 2', 53.50726,-1.04641],
    ['location 3', 53.48313,-1.01016],
    ['location 4', 53.48197,-1.00954],
    ['location 5', 53.48319,-1.00842]
];



var infoWindowContent = [
    ['<div class="info_content">' +
    '<strong>text 1' +    
    '</div>'],
    ['<div class="info_content">' +
    '<strong>text 2' +    
    '</div>'],
    ['<div class="info_content">' +
    '<strong>text 3' +    
    '</div>'],
    ['<div class="info_content">' +
    '<strong>text 4' +    
    '</div>'],
    ['<div class="info_content">' +
    '<strong>text 5' +    
    '</div>']
];

// Display multiple markers on a map
var infoWindow = new google.maps.InfoWindow(), marker, i;

// Loop through our array of markers & place each one on the map  
for( i = 0; i < markers.length; i++ ) {
    var position = new google.maps.LatLng(markers[i][1], markers[i][2]);
    bounds.extend(position);
    marker = new google.maps.Marker({
        position: position,
        map: map,
        title: markers[i][0]
    });

    // Allow each marker to have an info window 
    google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
            infoWindow.setContent(infoWindowContent[i][0]);
            infoWindow.open(map, marker);
        }
    })(marker, i));

    // Automatically center the map fitting all markers on the screen
    map.fitBounds(bounds);
}

// Override our map zoom level once our fitBounds function runs (Make sure it only runs once)
var boundsListener = google.maps.event.addListener((map), 'bounds_changed', function(event) {
    this.setZoom(13);
    google.maps.event.removeListener(boundsListener);
});
函数初始化(){
var映射;
var bounds=new google.maps.LatLngBounds();
变量映射选项={
中心:新google.maps.LatLng(53.47921,-1.00201),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“地图画布”),mapOptions);
地图.设置倾斜(45);
//停车场
变量标记=[
[位置1',53.47921,-1.00201],
[位置2',53.50726,-1.04641],
[位置3',53.48313,-1.01016],
[位置4',53.48197,-1.00954],
[位置5',53.48319,-1.00842]
];
var infoWindowContent=[
['' +
“文本1”+
''],
['' +
“文本2”+
''],
['' +
“文本3”+
''],
['' +
“文本4”+
''],
['' +
“文本5”+
'']
];
//在地图上显示多个标记
var infoWindow=new google.maps.infoWindow(),marker,i;
//在我们的标记阵列中循环并将每个标记放置在地图上
对于(i=0;i
}

任何帮助都会很好,谢谢

我对你的例子做了一些修改

  • 增加了样式,适当的高度和宽度
  • 缺少初始缩放级别
  • infoWindowContent现在只是字符串数组,标记已更正(
    丢失)
  • LatLngBounds():它需要SW和NE点。更改为使用
    bounds.extend()
  • infoWindow现在是在一个名为from
    for
    loop的函数中创建的

下面是在页面加载时打开第一个信息窗口的窗口。您可以将其更改为打开其中任何一个。

感谢上面的说明:)是否有方法在加载地图时默认打开第一个标记?谢天谢地。但是代码必须稍加修改才能保存所有标记和infoWindow。然后将
单击
事件附加到所有这些事件,并将
加载
仅附加到其中一个。@user3022336:我更改了代码,以便标记和信息窗口可以在数组中使用