如何在加载时触发第一个infowindow?

时间:2015-07-09 10:38:17

标签: wordpress google-maps google-maps-api-3 advanced-custom-fields

我已经对这个问题进行了很多搜索。而我似乎无法解决这个问题。更具体地说,我没有看到特定于acf的解决方案。而且我不想重写所有的j以使其工作:)

我希望在页面加载时触发我的reapeater字段的第一个标记的infowindow。

这是我的js代码,我从acf doc获取并适应了我的需求:

    <script>
      (function($) {
        function render_map( $el ) {

          var $markers = $el.find('.marker');

          var styles = [
            {
              "featureType": "road",
              "stylers": [
                { "visibility": "simplified" },
              ]
            },{
              "featureType": "poi",
              "stylers": [
                { "visibility": "off" }
              ]
            },{
              "featureType": "landscape.natural.terrain",
              "stylers": [
                { "visibility": "off" }
              ]
            }
          ];

          var args = {
            zoom        : 16,
            center      : new google.maps.LatLng(0, 0),
            mapTypeId   : google.maps.MapTypeId.ROADMAP,
            zoomControl: true,
            zoomControlOptions: {
              style: google.maps.ZoomControlStyle.SMALL,
              position: google.maps.ControlPosition.RIGHT_BOTTOM
            },
            panControl: false,
            mapTypeControl: false,
            scaleControl: false,
            streetViewControl: false,
            overviewMapControl: false,
            scrollwheel: false,
            mapTypeControlOptions: {
              mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'tehgrayz']
            }
          };

          // create map
          var map = new google.maps.Map( $el[0], args);
          var mapType = new google.maps.StyledMapType(styles, { name:"Grayscale" });
          map.mapTypes.set('tehgrayz', mapType);
          map.setMapTypeId('tehgrayz');

          // add a markers reference
          map.markers = [];

          // add markers
          $markers.each(function(){
              add_marker( $(this), map );
          });

          // center map
          center_map( map );
        }

        /*
        *  add_marker
        */
        function add_marker( $marker, map ) {
          // var
          var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );

          // create marker
          var marker = new google.maps.Marker({
              position : latlng,
              map : map,
              icon: '<?php bloginfo('template_url'); ?>/img/marker.svg'
          });

          // add to array
          map.markers.push( marker );

          // if marker contains HTML, add it to an infoWindow
          if( $marker.html() ){
            // create info window
            var infowindow = new google.maps.InfoWindow({
              content       : $marker.html()
            });

            // show info window when marker is clicked
            google.maps.event.addListener(marker, 'click', function() {
              infowindow.open( map, marker );
            });
          }
        }

        /*
        *  center_map
        */
        function center_map( map ) {

          // vars
          var bounds = new google.maps.LatLngBounds();

          // loop through all markers and create bounds
          $.each( map.markers, function( i, marker ){
            var latlng = new google.maps.LatLng( marker.position.lat(), marker.position.lng() );
            bounds.extend( latlng );
          });

          // only 1 marker?
          if( map.markers.length == 1 ){
            // set center of map
              map.setCenter( bounds.getCenter() );
              map.setZoom( 16 );
          }else{
            // fit to bounds
            map.fitBounds( bounds );
          }
        }

        /*
        *  This function will render each map when the document is ready (page has loaded)
        */
        $(document).ready(function(){
          $('.acf-map').each(function(){
            render_map( $(this) );
          });
        });
      })(jQuery);
    </script>

这是我的PHP代码循环遍历reapeter元素:

      <?php if( have_rows('locations') ): ?>
        <div class="acf-map" id="map">
          <?php $count=0;while ( have_rows('locations') ) : the_row();
              $location = get_sub_field('location');
          ?>
            <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>">
              <h4><?php the_sub_field('title'); ?></h4>
              <p class="address"><?php echo $location['address']; ?></p>
              <p><?php the_sub_field('description'); ?></p>
            </div>
        <?php endwhile; ?>
        </div>
      <?php endif; ?>

我有点迷失:/

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

传递给Configure Managed Projects的第一个参数 - 回调是集合中元素的索引。 检查索引是否为$.each,如果是,请触发0

click