简体   繁体   English

页面不刷新数据

[英]Page doesn't refresh the data

I am loading a Google Map using JS with the following code. 我正在使用带有以下代码的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();
  });

}); });

I'm creating the map and the markers using PHP 我正在使用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>

Then I'm taking input from the user and decode it to be pushed to a database. 然后,我从用户处获取输入并对其进行解码以将其推送到数据库。

<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'
      });
    }
  });
}

The data gets pushed into the database as it should, additional code required and the marker gets added. 数据将按原样推送到数据库中,需要附加代码并添加标记。 However, when the page refreshes, it requires another refresh to get the new data even though the data in the database gets inserted straight away. 但是,刷新页面时,即使立即插入数据库中的数据,也需要再次刷新才能获取新数据。

I have tried different refreshes but none of them fixed the problem. 我尝试了不同的刷新,但是都没有解决问题。

One of my solutions was to find a way to refresh the page twice but that would not be efficient enough since I have to load in hundreds of markers. 我的解决方案之一是找到一种刷新页面两次的方法,但这效率不够高,因为我必须加载数百个标记。

I answered my question. 我回答了我的问题。

The problem was, from what I'm guessing, the SQL query was being run slower than the page was being refreshed. 问题是,根据我的猜测,SQL查询的运行速度比刷新页面的速度慢。

To fix it, I added a delay. 为了解决这个问题,我添加了一个延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM