2017-07-06 65 views
0

我正在构建一个网页,其中包含一个显示Google地图上所有位置(自定义帖子类型)的页面。我使用ACF(高级自定义字段)将地图位置添加到特定位置。此外,我给每个位置一个类别(停车场,酒店等)。使用ACF在谷歌地图上切换Wordpress位置

此时所有位置都正确显示在地图上。但我试图找到一种方法来根据他们的类别切换这些位置。所以最初我想隐藏地图上的所有标记,并根据点击切换其可见性(锚点或复选框,无关紧要)。

我确实看到了一些看起来很有希望的答案,但是我无法用ACF提供的代码来解决这个问题。我希望有人能帮助我。并且可以告诉我在setvisibility的哪个位置添加一些额外的代码,并根据类别选择进行切换。

代码通过ACF提供如下图所示:

<script type="text/javascript"> 
(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); 

     }); 
    } 

} 

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

    }); 

}); 

})(jQuery); 
</script> 

呈现我的标记代码如下:

<div class="acf-map"> 

    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> 

     <?php 
      $location = get_field('google_maps_adres'); 
      if(!empty($location)): 
     ?> 

     <div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"> 
      <h4 class="marker__title"><?php the_title(); ?></h4> 
      <div class="marker__address"> 
       <?php $contact_address = get_field('google_maps_adres');$address = explode("," , $contact_address['address']); ?> 
       <div class="marker__address-street"><?php echo $address[0].', ';?></div> 
       <div class="marker__address-city"><?php echo $address[1];?></div> 
      </div> 
      <a class="marker__link" href="<?php the_permalink();?>">Meer informatie</a> 
     </div> 

     <?php endif; ?> 

    <?php endwhile;?> 

    <?php wp_reset_postdata(); ?> 

    </div> 

回答

0

我可以在这方面帮助,但也许它已经回答here。看看并尝试在那里提出的解决方案。如果你让我知道,我们能解决起来不工作;)

编辑:

好,let's尝试这样做则。首先,你需要区分每个标记,一个类或一个ID。让我们用类来做,因为我们使用的是类别,每个标记可以属于几个类别。

<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> 

<?php $cats = get_the_category(); //this gives me the categories of the post (marker) ?> 
<?php $catsString = implode(", ",$cats); // this gives me the categories as a String separated with ", " ?> 

    <?php 
     $location = get_field('google_maps_adres'); 
     if(!empty($location)): 
    ?> 

    <div class="marker <?php echo $catsString; ?>" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"> 
     <h4 class="marker__title"><?php the_title(); ?></h4> 
     <div class="marker__address"> 
      <?php $contact_address = get_field('google_maps_adres');$address = explode("," , $contact_address['address']); ?> 
      <div class="marker__address-street"><?php echo $address[0].', ';?></div> 
      <div class="marker__address-city"><?php echo $address[1];?></div> 
     </div> 
     <a class="marker__link" href="<?php the_permalink();?>">Meer informatie</a> 
    </div> 

    <?php endif; ?> 

<?php endwhile;?> 

所有我所做的是每个标记得到的类别,并用它们作为类为主要标志的容器:

如下更改代码。

现在,您已经将所有标记与其类设置在一起。那很好。下一步是使用一些输入来控制这些标记的可见性。例如,我们使用复选框。

对于每个类别,你应该有一个复选框:

<?php $categories = get_categories(); 
     foreach($categories as $category) { 
      echo '<label><input class="markerToggle" type="checkbox" id="'. $category->name .'">'. $category->name .'</label>'; 
     } 
?> 

那么你就需要一些简单的JavaScript来隐藏/显示每个标记

$('.markerToggle').change(function() { 
    if($(this).is(":checked")) { 
     //'checked' event code 
     $('.marker.' + this.id).show(); 
     return; 
    } 
    $('.marker.' + this.id).hide(); 
    //'unchecked' event code 
}); 
+0

感谢名单你很而不更迭。可能是因为我对这个主题缺乏认识。所以我不知道在哪里提供任何代码。我已经把JS代码放在我原来的问题中。所以现在我已经添加了一些代码来显示我的标记是如何在页面上打印的。 – Toasty

+0

看到我编辑的答案。 –

+0

非常感谢您的编辑。不幸的是我仍然无法让它工作。我应该把JavaScript放在哪里?在这一点上,我尝试了我地图内容中的不同位置。但我想我应该去add_marker部分。在这一点上,我不会默认隐藏所有标记。不知道我做错了什么。 – Toasty