2017-08-01 111 views
0

我正在使用高級自定義字段添加谷歌地圖標記在WordPress中。我有一套過濾器來對標記進行分類。我的目標是僅顯示選中過濾器的地圖標記。我發現了堆棧溢出的幾個例子,但這些例子中的大多數都使用json數據或靜態數組。我的JS技能不在我可以將其轉換成我的情況的水平。過濾谷歌地圖標記與複選框和高級自定義字段

下面是過濾器,這將是地圖上方的HTML:

<article class="map-filters"> 
    <div class="map-filters__inner"> 
     <ul class="map-filters__wrap"> 
      <li><label for="category1"><input type="checkbox" name="filter" value="category1" id="category1" checked>Category 1</label></li> 
      <li><label for="category2"><input type="checkbox" name="filter" value="category2" id="category2" checked>Category 2</label></li> 
      <li><label for="category3"><input type="checkbox" name="filter" value="category3" id="category3" checked>Category 3</label></li> 
     </ul> 
    </div> 
</article> 

這裏是地圖本身的HTML。同樣,我使用ACF中繼域動態添加地圖標記,但我在這個例子的目的而提供的靜態HTML:

<article id="js-community-map" class="community-map"> 
    <div class="marker" data-category="category1" data-icon="category1.png" data-lat="35.9491598" data-lng="-115.1354093">Marker Content</div> 
    <div class="marker" data-category="category2" data-icon="category2.png" data-lat="35.9320515" data-lng="-115.1236344">Marker Content</div> 
    <div class="marker" data-category="category3" data-icon="category2.png" data-lat="35.9327263" data-lng="-115.1246636">Marker Content</div> 
</article> 

最後,是JS。由於使用了高級自定義字段,您會發現它與典型的Google Maps JS略有不同,但應該非常相似。我遇到的問題是我不知道在這段代碼中添加過濾功能的位置。我的過濾代碼也可能不正確。我曾嘗試在幾個地方添加過濾,包括new_map函數,但似乎沒有任何工作。如果有人能幫助我解決這個問題,將不勝感激。

(function($) { 

    // New Map 
    // This function will render a Google Map onto the selected jQuery element 
    function new_map($el) { 

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

     // args 
     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 
    function add_marker($marker, map) { 

     // vars 
     var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng')); 
     var category = $marker.data('category'); // Get category name from data 

     // create marker 
     var marker = new google.maps.Marker({ 
      position : latlng, 
      map   : map, 
      category : category, // store category as property of marker 
      icon  : iconUrl.image + $marker.attr('data-icon') // Custom icon from data attribute 
     }); 

     // 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() { 
       map.panTo(this.getPosition()); 
       map.setZoom(15); 
       infowindow.open(map, marker); 
      }); 
     } 
    } 

    // Center Map 
    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  
    // global var 
    var map = null; 
    $(document).ready(function(){ 
     $('#js-community-map').each(function(){ 

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

     }); 
    }); 
})(jQuery); 

這是我正在嘗試使用的過濾功能。請讓我知道是否需要更改此代碼。另外,如果你能幫我把它放在上面的JS中,這將是非常有幫助的。

$(document).on('click', '.map-filters__wrap label', function(){ 
    $.each(map.markers, function(i, marker) { 
     if(marker.category == category) { 
      marker.visible = true; 
     } else { 
      marker.visible = false; 
     } 
    }); 
}); 

回答

1

對於需要使用ACF Google地圖進行過濾的用戶,我可以自己弄清楚這一點!

這裏是更新JS與過濾器的工作:

(function($) { 

    // New Map 
    // This function will render a Google Map onto the selected jQuery element 
    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); 
     }); 

     // Filter Markers 
     $('.map-filters__wrap').on('change', 'input[type="checkbox"]', function ({ 
      filter = $(this); 
      filterValue = filter.val(); 
      if(filter.is(':checked')) { 
       map.markers.forEach(function(element) { 
        if(element.category == filterValue) { 
         element.setVisible(true); 
        } 
       }); 
      } else { 
       map.markers.forEach(function(element) { 
        if(element.category == filterValue) { 
         element.setVisible(false); 
        } 
       }); 
      } 
     }); 

     // center map 
     center_map(map); 

     // return 
     return map; 
    } 


    // Add Marker 
    function add_marker($marker, map) { 
     // var 
     var latlng = new google.maps.LatLng($marker.attr('data-lat'), $marker.attr('data-lng')); 
     var category = $marker.data('category'); // Get category name from data 

     // create marker 
     var marker = new google.maps.Marker({ 
      position : latlng, 
      draggable : true, // set marker to draggable to hide duplicates 
      crossOnDrag : false, // hide cross icon on drag event 
      map   : map, 
      category : category, // store category as property of marker 
      icon  : iconUrl.image + $marker.attr('data-icon') // Custom icon from data attribute 
     }); 

     // 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 
    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 
    // global var 
    var map = null; 
    $(document).ready(function(){ 
     $('#js-community-map').each(function(){ 
      // create map 
      map = new_map($(this)); 
     }); 
    }); 
})(jQuery); 
0

感謝您的例子,我是新來這,這是非常有幫助!

只是一個小的觀察,可以做一些其他新手有點迷惑,有一個非常小的語法錯誤的位置:

篩選標記

$('.map-filters__wrap').on('change', 'input[type="checkbox"]', function ({ 

有一個)失蹤那裏,應該是:

過濾器標記

$('.map-filters__wrap').on('change', 'input[type="checkbox"]', function() { 
+0

這是非常有用的信息在答案本身。我建議你編輯答案以解決語法錯誤的問題,這種方式總是讓人們在將來看到,而不必滾動瀏覽所有答案。 – Taegost

+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/18669589) – etarion