Markers not appearing on Google Map using wp_query

2019-08-22 23:02发布

问题:

I am using the WP Favorite Posts plugin to allow users to select their favourite tours on the site. The posts are saved using to cookies to a saved template provided by the plugin. I have edited this template to include a map and to pull the coordinates from a Custom Meta Field.

The full template can be found at http://pastebin.com/zDrr5fPn.

The code I have included to display the map is:

<div id="map" style="width: 100%; height: 250px; position: relative; overflow: hidden; -webkit-transform: translateZ(0px); background-color: rgb(229, 227, 223);"></div>

and the code I am using for the loop is:

while ( $loop->have_posts() ) : $loop->the_post();
                if ( get_post_meta($post->ID, 'custom_latlng', true) !== '' ) : ?>
                    <div style="display:block;">
                        <script type="text/javascript">
                            function initialize() {
                            //load map
                            map = new google.maps.Map(document.getElementById('map'), { 
                                  zoom: 9, 
                                  center: new google.maps.LatLng(53.3498, -6.2603), 
                                  mapTypeId: google.maps.MapTypeId.ROADMAP,
                                  disableDefaultUI: true
                            });


                            var savedMarkers = new Array();



                             <?php $saved_pos = get_post_meta($post->ID, 'custom_latlng', true);?>

                                function addMarker() {
                                    var savedMarker = new google.maps.Marker({
                                        map: map,
                                        position: new google.maps.LatLng(<?php echo $saved_pos ?>),
                                        icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
                                    });
                                savedMarkers.push(savedMarker);

                                }
                                }
                        </script>
                    </div>

At the moment, when I view the source, I can see the points being selected, the coordinates do appear. However they do not appear on the map itself. It is as if the points are appearing in the list of saved posts but not on the map at all.

I hope this makes sense.

Cheers

回答1:

Inside the loop only populate an array with the latitudes/longitudes, create initialize outside of the loop:

<div id="map" style="width: 100%; height: 250px;"></div>

<script type="text/javascript">
  function initialize() {
    //load map
    map = new google.maps.Map(document.getElementById('map'), { 
                               zoom: 9, 
                               center: new google.maps.LatLng(53.3498, -6.2603), 
                               mapTypeId: google.maps.MapTypeId.ROADMAP,
                               disableDefaultUI: true
         });
    //create the markers
    for(var i=0;i<savedMarkers.length;++i){
      savedMarkers[i] = new google.maps.Marker({
             map: map,
             position: new google.maps.LatLng(savedMarkers[i][0],
                                              savedMarkers[i][1]),
             icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
           });
    }
  }

<?php
  //create a php-array to store the latitudes and longitudes
  $savedMarkers=array();
    //use the loop to populate the array
    while ( $loop->have_posts() ) : $loop->the_post();
      if ( get_post_meta($post->ID, 'custom_latlng', true) !== '' ) : 
        $savedMarkers[]=explode(',',get_post_meta($post->ID, 'custom_latlng', true));
      endif;
    endwhile;
?>
//print the array as js-variable
savedMarkers= <?php echo json_encode($savedMarkers);?>;

</script>

It's not tested, there may be some errors, but it should be sufficient to demonstrate the workflow.


Related to the comments: To apply the post-title as infowindow-content also store the title in the savedMarkers-items:

$savedMarkers[]=array_merge(explode(',',get_post_meta($post->ID, 'custom_latlng', true)),
                            array(get_the_title()));

when you create the marker create a custom property for the marker where you store the infowindow-content(let's call the property content):

//create the markers
for(var i=0;i<savedMarkers.length;++i){
  savedMarkers[i] = new google.maps.Marker({
         map: map,
         position: new google.maps.LatLng(savedMarkers[i][0],
                                          savedMarkers[i][1]),
         icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
          //the content(post-title)
         content: savedMarkers[i][2]
       });
}

Now use this content as infowindow-content:

google.maps.event.addListener(savedMarkers[i], 'click', function() {
    infowindow.setContent(this.get('content'));
    infowindow.open(this.getMap(), this);
  }
);

You may also create a link to the posts inside the infowindows:

$savedMarkers[]=array_merge(explode(',',get_post_meta($post->ID, 'custom_latlng', true)),
                            array(get_the_title(),get_permalink()));

......

//create the markers
for(var i=0;i<savedMarkers.length;++i){
  savedMarkers[i] = new google.maps.Marker({
         map: map,
         position: new google.maps.LatLng(savedMarkers[i][0],
                                          savedMarkers[i][1]),
         icon: '/wp-content/themes/dublin-visitors-centre/images/saved_icon.png',
         //the content(post-title)
         title: '' + savedMarkers[i][2],
         //post-URL
         href: savedMarkers[i][3]
       });
}

..........

google.maps.event.addListener(savedMarkers[i], 'click', function() {
    var link=document.createElement('a');
    link.appendChild(document.createTextNode(this.get('title')));
    link.setAttribute('href',this.get('href'));
    infowindow.setContent(link);
    infowindow.open(this.getMap(), this);
  }
);