将 acf 字段传递给 Leaflet JS

Pass acf fields to Leaflet JS

我在 Internet 上找到了这个示例,它非常接近我想要做的事情。 我有一些带有某些标记的经纬度的 ACF 字段,我试图在同一张地图中显示自定义 post 类型地点的所有标记。 但是我只得到1,我做错了什么?

我认为与标记有关 div

<?php /* Template Name: Demo Page Template */ get_header(); ?>
<h1 class="main-title"><?= post_type_archive_title(); ?></h1>

<section class="list-cases">
  <div class="container">
    <div class="row position-relative">
      <?php
        $args = array(
          'post_type' => 'lugar',
          'posts_per_page' => 10,
        );
        $query = new WP_Query( $args );
        ?>
      <?php $counter = 1; ?>
      <?php while( $query->have_posts() ) : $query->the_post() ?>
      <div class="item-case-<?= $counter ?> col-md-12" data-hover="<?= $counter ?>" data-aos="fade-up"
        data-aos-duration="2000">
        <div class="case-container">
          <?php the_title( '<h2 class="entry-title"><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0' ) . '" rel="bookmark">', '</a></h2>' ); ?>
          <p><?php the_field('subtitle') ?></p>
          <p><?php the_field('address') ?></p>
          <p><strong>Latitud:</strong> <?php the_field('lat') ?></p>
          <p><strong>Longitud:</strong> <?php the_field('lon') ?></p>
        </div>
      </div>
      <?php $counter++; ?>
      <?php endwhile; wp_reset_postdata(); ?>
    </div>
  </div>
</section>


<style>
  #map,
  #acf-map {
    height: 500px;
  }
</style>
<div id="map" style="width: 600px;">


</div>
<div class="marker" data-lat="<?php echo the_field('lat') ?>" data-lng="<?php the_field('lon'); ?>"
  data-title="<?php the_field('subtitle')  ?>" data-url="<?php echo 'link'; ?>"
  data-category="<?php echo 'category'; ?>"></div>


<script>
  $(document).ready(function () {
    var map = L.map('map').setView([39.8410802, -0.1191397], 8);

    L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}', {
      attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
      maxZoom: 18,
      id: 'mapbox/streets-v11',
      tileSize: 512,
      zoomOffset: -1,
      accessToken: 'pk.eyJ1IjoiamFtYWxkb2xzIiwiYSI6ImNrYmF2bGowOTBycGEyeG84b2F2NGlsYWkifQ.9rqymFnsW79aCkAFGCo0XQ'
    }).addTo(map);


    var marker = [];
    $.each($('.marker'), function () {
      var lat = $(this).attr('data-lat');
      var lng = $(this).attr('data-lng');
      var name = $(this).attr('data-title');
      var link = $(this).attr('data-url');
      console.log('Marker es: ' + marker);
      marker = [name, lat, lng, link];
      console.log('Y ahora es: ' + marker);
      console.log(marker);
    });

    var markers = [marker];
    console.log(markers);
    console.log(markers.length);
    for (var i = 0; i < markers.length; i++) {
      marker = new L.marker([markers[i][1], markers[i][2]])
        .bindPopup(markers[i][0])
        .addTo(map);
    }
  });
</script>

请确保您是echo-ing自定义字段值。 示例:data-lat="<?php echo get_field('lat'); ?>"data-lat="<?php echo get_field('lon'); ?>"

您需要将 div.marker 放入循环中以显示具有自定义 lat-long 值的多个标记。

<?php while( $query->have_posts() ) : $query->the_post() ?>
  <div class="marker" 
    data-lat="<?php echo get_field( 'lat' ) ?>" 
    data-lng="<?php echo get_field( 'lon' ); ?>"
    data-title="<?php echo get_field( 'subtitle' )  ?>" 
    data-url="<?php echo 'link'; ?>"
    data-category="<?php echo 'category'; ?>" >
  </div>
<?php endwhile; wp_reset_postdata(); ?>

此外,我会修改您的 javascript,使其看起来像这样:

var markers = [];

$.each($('.marker'), function () {
    var marker = [];
    var lat = $(this).attr('data-lat');
    var lng = $(this).attr('data-lng');
    var name = $(this).attr('data-title');
    var link = $(this).attr('data-url');
    console.log('Marker es: ' + marker);
    marker = [name, lat, lng, link];
    console.log('Y ahora es: ' + marker);
    console.log(marker);
    markers.push( marker );
});


console.log(markers);
console.log(markers.length);
for (var i = 0; i < markers.length; i++) {
    marker = new L.marker([markers[i][1], markers[i][2]])
    .bindPopup(markers[i][0])
    .addTo(map);
}