繁体   English   中英

使用wp_query查找点的标记未显示在Google地图上

[英]Markers not appearing on Google Map using wp_query to find points

我正在使用WP Favorite Posts插件,以允许用户在网站上选择自己喜欢的游览。 帖子使用cookie保存到插件提供的已保存模板。 我已经编辑了此模板,以包括地图并从“自定义元字段”中提取坐标。

完整的模板可以在http://pastebin.com/zDrr5fPn中找到。

我包含的用于显示地图的代码是:

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

我在循环中使用的代码是:

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>

此刻,当我查看源时,可以看到被选择的点,确实会出现坐标。 但是,它们不会出现在地图本身上。 这些点似乎出现在已保存的帖子列表中,但根本不在地图上。

我希望这是有道理的。

干杯

在循环内部,仅使用纬度/经度填充数组,在循环外部创建initialize

<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>

它未经测试,可能会有一些错误,但是足以证明工作流程。


与评论有关:要将帖子标题应用为信息窗口内容,还将标题存储在savedMarkers-items中:

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

当您创建标记时,请为存储信息窗口内容的标记创建一个自定义属性(我们将属性称为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]
       });
}

现在将此内容用作infowindow-content:

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

您也可以在信息窗口内创建指向这些帖子的链接:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM