如何使用Google Maps Javascript API V3加载infoWindow内容

时间:2014-02-07 04:44:58

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

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

我正在使用以下Google地图示例 - 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);

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

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

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

这就是我如何知道在添加标记之后在哪里使用panTo()地图,但我无法弄清楚如何使用setContent()将infoBox添加到此标记,如果这甚至是我需要解决的问题。任何人都可以帮我在第一个标记上获取infoBox的内容集,然后打开infoBox作为地图的“开放状态”吗?

1 个答案:

答案 0 :(得分:1)

可能的选项

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