google-maps - 如何使用 Google Maps Javascript API V3 在加载时设置信息窗口内容

标签 google-maps google-maps-api-3 infowindow

现在我正在为一个客户的网站制作“位置”页面,该页面将其四个位置的名称显示为链接。链接列表下方是 Google map Canvas ,其中为每个位置添加了标记和信息窗口。加载时, map 默认为第一个位置标记的位置,并且应该打开该标记的信息窗口。单击位置名称应将 map 平移到相应的标记并打开信息窗口。

我几乎完全使用以下 Google map 示例 - http://wrightshq.com/playground/placing-multiple-markers-on-a-google-map-using-api-3/我已经注释掉了 map.fitBounds(bounds);行和下面的整个 boundsListener。在监听器的位置,我添加了 map.panTo() 以转到第一个标记的位置,并将 zoom:14 添加到 mapOptions。这是我的完整代码:

function initialize() {
    var map;
    var bounds = new google.maps.LatLngBounds();

    // Define some styles for the colors/look.
    // Generate styles here: http://gmaps-samples-v3.googlecode.com/svn/trunk/styledmaps/wizard/index.html
    var styles = [ { "featureType": "landscape.man_made", "elementType": "geometry", "stylers": [ { "color": "#abce80" } ] },{ "featureType": "landscape.natural", "elementType": "geometry", "stylers": [ { "color": "#749f71" } ] },{ "featureType": "road", "elementType": "geometry", "stylers": [ { "color": "#99c26e" }, { "weight": 0.6 } ] },{ "featureType": "road.highway", "elementType": "geometry", "stylers": [ { "color": "#eaf5a1" }, { "weight": 1.5 } ] },{ "featureType": "road", "elementType": "labels.text.fill", "stylers": [ { "color": "#5b5b3e" } ] },{ "featureType": "road", "elementType": "labels.text.stroke", "stylers": [ { "color": "#a9d07f" } ] },{ "featureType": "poi", "elementType": "labels.text.stroke", "stylers": [ { "color": "#f5ffa8" } ] },{ "featureType": "water", "elementType": "geometry", "stylers": [ { "color": "#aee3e0" } ] },{ "featureType": "poi", "elementType": "geometry", "stylers": [ { "color": "#806e4e" }, { "lightness": 27 } ] },{ } ];

    var mapOptions = {
        mapTypeId: 'roadmap',
        styles: styles,
        zoom:14
    };

    // Display a map on the page
    map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

    // Multiple Markers
    var markers = [
    <?php if( get_field('locations') ): ?>
        <?php while( has_sub_field('locations') ): $map_info = get_sub_field('map_location'); ?>
        ['<?php echo $map_info['address'] ?>',<?php echo $map_info['lat'] ?>,<?php echo $map_info['lng'] ?>],
        <?php endwhile; ?>
    <?php endif; ?>
    ];
    mapCenter = new google.maps.LatLng(markers[0][1],markers[0][2]);
    //console.log(mapCenter);

    // Info Window Content
    var infoWindowContent = [
    <?php if( get_field('locations') ): ?>
        <?php while( has_sub_field('locations') ): ?>
        ['<div class="google-map-tooltip">' +
        '<strong><?php echo get_sub_field('pin_heading'); ?></strong>' +
        '<p><?php echo str_replace('<br />','<br />\\',get_sub_field('address')); ?></p>' +
        '</div>'],
        <?php endwhile; ?>
    <?php endif; ?>

    ];

    // 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],
            icon: '<?php echo IMAGES; ?>/icon-map-marker.png',
        });

        if( i==0 ){ first_marker = marker; }

        // 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);
    }


        //Move map to first tab's location
        map.panTo(mapCenter);
        console.log(first_marker);
        infoWindow.open(map,first_marker);

    // 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(14);
        google.maps.event.removeListener(boundsListener);
    });*/

}//END initialize()
google.maps.event.addDomListener(window, 'load', initialize);

代码的工作原理是添加了标记并且 map 从正确的标记开始。如果单击标记,则会显示正确的信息窗口。但是,我希望发生的是,不仅 map 从第一个标记位置开始,而且还需要打开标记 infoWindow。您会在该示例代码中注意到的问题是,在标记的点击监听器中调用 infoWindow.setContent() 之前,永远不会设置 infoWindow 内容。换句话说,在单击它们之前,信息窗口实际上没有任何内容。因此,当我尝试在首次显示 map 时在第一个标记上使用 infoWindow.open() 时,从技术上讲它会打开 infoWindow,但您只能看到工具提示点而没有其余的工具提示气泡,因为内容还没有尚未通过点击事件添加。

ma​​rker 被声明为新的 Marker 对象之后,我通过将这一行添加到 for 循环中来确定第一个标记:

if( i==0 ){ first_marker = marker; }

这就是我知道在添加标记后在哪里使用 panTo() map 的方式,但我无法弄清楚如何使用 setContent() 向该标记添加信息框,如果这就是我需要解决的问题的话.谁能帮我在第一个标记上获取信息框的内容集,然后将信息框打开为 map 的“打开状态”?

最佳答案

可能的选择

设置infoWindow.open()之前的内容

infoWindow.setContent(infoWindowContent[0][0]);
infoWindow.open(map,first_marker);

触发对标记的点击:

google.maps.event.trigger(first_marker,'click');

设置选项:

 infoWindow.setOptions({
  content:infoWindowContent[0][0],
  map:map,
  position:first_marker.getPosition()
});

关于google-maps - 如何使用 Google Maps Javascript API V3 在加载时设置信息窗口内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21619531/

相关文章:

javascript - AngularJS Google Map 指令 map 实例

android - JQuery Mobile 和谷歌地图故障

javascript - 使用 Google Maps API 在香港定位 2 个位置

jquery - 使用ajax v3的谷歌地图信息窗口

google-maps - Google Maps API v3 中的 GInfoWindowTab 等效项

java - google-play-services 不会构建到 Eclipse 中的 Android Maps HelloWorld 项目中

java - Vaadin 添加带有纬度和经度的图像 map

javascript - 谷歌地图打印如何工作?

javascript - 使用信息窗口点击时的 Google Maps API v3 标记

javascript - Google map 上的自定义路线/路径/道路