使用div在地图外部触发Google Map InfoWindow

时间:2018-08-05 21:10:49

标签: javascript php google-maps advanced-custom-fields infowindow

我有一个Google地图,显示用户输入的位置(旅行计划)。单击标记后,地图和位置显示良好,标记infoWindow显示良好。

在地图下方,我还列出了旅行计划。这些也显示正常。但是,当单击正确的旅行计划时,我希望弹出相关的infoWindow。

对于我的一生,我无法弄清楚-非常感谢您的帮助!

这是我的PHP

<?php 
        $the_query_map = new WP_Query( array( 'post_type' => 'travel_plans', 'posts_per_page' => -1, 'order' => 'DESC', 'meta_key' => 'date_from', 'orderby' => 'meta_value_num' ) );

        if($the_query_map->have_posts()) :
        while($the_query_map->have_posts()):
        $the_query_map->the_post();
        $the_ID = get_the_ID();
        $get_google_map = get_field('location');
        $title = get_the_title();
        $description = get_field('description');
        $date_from = get_field('date_from');
        $date_to = get_field('date_to');
        $datedeb = get_field('date_from', false, false);
        $datefin = get_field('date_to', false, false);

        um_fetch_user(get_current_user_id());

        if (is_user_logged_in() && (get_current_user_id() == $post->post_author)) {
        ob_start(); ?>
        <div>
        <p><a onclick="return confirm('Are you SURE you want to delete this travel plan?')" href="<?php echo get_delete_post_link( $post->ID ) ?>">Delete post</a></p>
        <?php } ?></div>
        <?php $delete_button = ob_get_clean();?>


        <?php
        $output_map[$the_ID]['map'] = '<div class="marker" data-lat="'.$get_google_map['lat'].'" data-lng="'.$get_google_map['lng'].'"><div class="acf-form"><p class="address"> '. $get_google_map['address'] .'</p> <div>'. $title .' </div><div> '. $description .' </div><div> '. $date_from .' - '. $date_to .' <div> '. $delete_button .' </div></div></div></div>';


        $today = strtotime(date('Ymd'));
        $promoend = strtotime($datefin);

        ?>          
             <?php if ($promoend >=$today): 
                  $output_text[$the_ID]['text'] = '<a href="#" class="marker" data-lat="'.$get_google_map['lat'].'" data-lng="'.$get_google_map['lng'].'">'. $get_google_map['address'] .', '. $title .', '. $description .' '. $date_from .' - '. $date_to .'</a>';
             endif; ?>
             <?php if ($promoend <$today): 
                  $output_text_old[$the_ID]['text'] = '<a href="#" class="marker" data-lat="'.$get_google_map['lat'].'" data-lng="'.$get_google_map['lng'].'">'. $get_google_map['address'] .', '. $title .', '. $description .' '. $date_from .' - '. $date_to .'</a>';
             endif;

        endwhile; endif;
        wp_reset_postdata();

        ?><div class="acf-map"><?php
        foreach( $output_map as $key => $map_marker ):?>
            <div class="acf-map">
                <?php echo $map_marker['map'];?>
            </div>
        <?php endforeach; ?>

        </div>
        <div style="background-color:#cccccc;">Upcoming trips</div>
        <?php
        foreach( $output_text as $key => $post_text ):?>
                    <div>

                       <?php echo $post_text['text'];?>

                    </div>
        <?php endforeach; ?>

        <div style="background-color:#cccccc;">These are my old trips</div>
        <?php
        foreach( $output_text_old as $key => $post_text ):?>
                    <div>

                    <?php echo $post_text['text'];?>


                    </div>
        <?php endforeach; ?>



    <?php $show_for_roles = array( 'um_journalist','um_admin','administrator' );
$profile_id = um_profile_id();
if ( is_user_logged_in() && get_current_user_id() == $profile_id && in_array( um_user('role') , $show_for_roles )  ) {?>


<h4>Add a location or travel plan</h4>

<?php 

acf_form(array(
            'post_id'       => 'new_post',
            'field_groups' => array(3275), // Used ID of the field groups here.
            'post_title' => true, 
            'new_post'      => array(
                'post_type'     => 'travel_plans',
                'post_status'       => 'publish',
            ),
            'submit_value'      => 'Add your travel plan'
        )); 
}?>

这是我的js

    (function($) {

/*
*  new_map
*
*  This function will render a Google Map onto the selected jQuery element
*
*  @type    function
*  @date    8/11/2013
*  @since   4.3.0
*
*  @param   $el (jQuery element)
*  @return  n/a
*/

function new_map( $el ) {

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


    // vars
    var args = {
        zoom        : 16,
        center      : new google.maps.LatLng(0, 0),
        mapTypeId   : google.maps.MapTypeId.ROADMAP
    };


    // create map               
    var map = new google.maps.Map( $el[0], args);


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


    // add markers
    $markers.each(function(){

        add_marker( $(this), map );

    });


    // center map
    center_map( map );


    // return
    return map;

}

/*
*  add_marker
*
*  This function will add a marker to the selected Google Map
*
*  @type    function
*  @date    8/11/2013
*  @since   4.3.0
*
*  @param   $marker (jQuery element)
*  @param   map (Google Map object)
*  @return  n/a
*/

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

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

        });

    }

}

// popup is shown and map is not visible
google.maps.event.trigger(map, 'resize');

/*
*  center_map
*
*  This function will center the map, showing all markers attached to this map
*
*  @type    function
*  @date    8/11/2013
*  @since   4.3.0
*
*  @param   map (Google Map object)
*  @return  n/a
*/

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

}


/*
*  document ready
*
*  This function will render each map when the document is ready (page has loaded)
*
*  @type    function
*  @date    8/11/2013
*  @since   5.0.0
*
*  @param   n/a
*  @return  n/a
*/
// global var
var map = null;

$(document).ready(function(){

    $('.acf-map').each(function(){

        // create map
        map = new_map( $(this) );

    });

$('.um-profile-nav-item a').on('shown', function (e) { 
      initialize(); // google map init function
  });

});

})(jQuery);

0 个答案:

没有答案