2017-07-06 60 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