簡體   English   中英

頁面不刷新數據

[英]Page doesn't refresh the data

我正在使用帶有以下代碼的JS加載Google Map。

$(document).ready(function(){
  $('.acf-map').each(function(){
    map = new_map( $(this) );
  });
  var geocoder = new google.maps.Geocoder();
  document.getElementById('submit').addEventListener('click', function() {
    geocodeAddress(geocoder, map);
    location.reload();
  });

});

我正在使用PHP創建地圖和標記

<div class="acf-map">
    <?php $custom = new WP_Query(array(
        'post_type'         => 'centre',
        'posts_per_page'    => -1

    )); ?>
    <?php
    if ($custom->have_posts()) {
        while ($custom->have_posts()) {
            $custom->the_post(); ?>
            <?php $location = get_field('centre_location'); ?>

            <div class="marker" data-lat="<?php echo $location['lat'] ?>" data-lng="<?php echo $location['lng']; ?>" style="border: 5px solid grey;">
                <a href="<?= the_permalink() ?>"><h5 class="orange"><?= get_field('centre_name'); ?></h5></a>
                <p class="address"><?php echo get_field('centre_address') ?></p>
                <p class="orange">Call us at <a href="tel: <?= get_field('centre_telephone_number'); ?>"><?= get_field('centre_telephone_number'); ?></a></p>

            </div>

            <?php
        }
        wp_reset_postdata();
    }
    ?>
</div>

然后,我從用戶處獲取輸入並對其進行解碼以將其推送到數據庫。

<input type="text" placeholder="Enter your postcode" class="orange find input" id="address">
<button id="submit" class="find sub orange">Search</button>

function geocodeAddress(geocoder, resultsMap) {
  var address = document.getElementById('address').value;
  geocoder.geocode({'address': address}, function(results, status) {
    if (status === 'OK') {
      resultsMap.setCenter(results[0].geometry.location);
      var lat = results[0].geometry.location.lat();
      var lng = results[0].geometry.location.lng();
      $.ajax({
        url: "../link/to/post.php",
        type: "POST",
        data : {
          lat: lat,
          lng: lng
        }
      });
      var marker = new google.maps.Marker({
        map: resultsMap,
        position: results[0].geometry.location,
        title: 'Your location'
      });
    }
  });
}

數據將按原樣推送到數據庫中,需要附加代碼並添加標記。 但是,刷新頁面時,即使立即插入數據庫中的數據,也需要再次刷新才能獲取新數據。

我嘗試了不同的刷新,但是都沒有解決問題。

我的解決方案之一是找到一種刷新頁面兩次的方法,但這效率不夠高,因為我必須加載數百個標記。

我回答了我的問題。

問題是,根據我的猜測,SQL查詢的運行速度比刷新頁面的速度慢。

為了解決這個問題,我添加了一個延遲。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM