根据半径限制引脚数量?

时间:2014-03-04 21:12:25

标签: php wordpress maps listings pins

下面的代码位于:/includes/search/functions/ajax.php line:1489

这是基于所选搜索字段的引脚在任何给定页面上填充地图的内容......

有人可以帮我弄清楚如何将此搜索限制在特定半径,以便地图上的每个图钉都不会加载吗?

现在所有的引脚都在地图上加载,消耗了分配的cpu功率,并且当超过1000个引脚立即加载时,最终可能会崩溃浏览器。

/////////////////////////////////////////////////////////////////
            //// ACTUALLY DOES THE QUERY
            /////////////////////////////////////////////////////////////////

            $sQ = new WP_Query($args);

            //// STARTS OUR POST ARRAY - EVERY FOUND POST IS INSERTED IN HERE IN ORDER TO ADD THE PINS
            $return['posts'] = array();
            $return['post_ids'] = array();

            /// LOOPS POSTS
            if($sQ->have_posts()) { while($sQ->have_posts()) { $sQ->the_post();

                ///// GETS REQUIRED FIELDS TO INSERT IN THE ARRAY
                $latitude = get_spot_latitude(get_the_ID());
                $longitude = get_spot_longitude(get_the_ID());
                $pin = get_spot_pin(get_the_ID());

                $featured = 'false';
                $thumb = '';

                //// IF FEATURED OVERLAYS ARE SET
                if(ddp('map_featured_overlay') == 'on') {

                    //// IF THIS IS FEATURED
                    if(get_post_meta(get_the_ID(), 'featured', true) == 'on') { $featured = 'true'; }
                    $thumb = ddTimthumb(btoa_get_featured_image(get_the_ID()), 150, 150);

                }

                //// ONLY ADDS TO THE ARRAY IN CASE WE HAVE A LATITUDE AND LONGITUDE
                if($latitude != '' && $longitude != '') {

                    $return['posts'][] = array(

                        'title' => get_the_title(),
                        'id' => get_the_ID(),
                        'latitude' => $latitude,
                        'longitude' => $longitude,
                        'pin' => $pin,
                        'permalink' => get_permalink(),
                        'featured' => $featured,
                        'thumb' => $thumb,

                    );

                    $return['post_ids'][] = get_the_ID();

                } else {

                    $return['posts'][] = array(

                        'title' => get_the_title(),
                        'error' => 'NO LATITUDE OR LONGITUDE'

                    );

                }//// ENDS IF POST HAS LATITUDE AND LONGITUDE

            } }

这是搜索字段的一部分:

<input type="hidden" id="_sf_enable_radius_search" value="false" name="_sf_enable_radius_search" />
                    <input type="hidden" id="_sf_radius_lat_from" value="" name="_sf_radius_lat_from" />
                   <input type="hidden" id="_sf_radius_lat_to" value="" name="_sf_radius_lat_to" />
                       <input type="hidden" id="_sf_radius_lng_from" value="" name="_sf_radius_lng_from" />
                   <input type="hidden" id="_sf_radius_lng_to" value="" name="_sf_radius_lng_to" />
                       <input type="hidden" id="_sf_radius_center_lat" value="" name="_sf_radius_center_lat" />
                       <input type="hidden" id="_sf_radius_center_lng" value="" name="_sf_radius_center_lng" />
                       <input type="hidden" id="_sf_radius_field" value="false" name="_sf_radius_field" />
                    <input type="hidden" id="_sf_radius_field_id" value="false" name="_sf_radius_field_id" />
                      <input type="hidden" id="_sf_post_ids" value="" name="_sf_post_ids" />


                       <input type="hidden" id="_sf_radius_distance" value="" name="_sf_radius_distance" />

                        <input type="hidden" name="is_taxonomy" value="true" id="_sf_search_is_taxonomy" />

2 个答案:

答案 0 :(得分:0)

$loopCount = 0;
$maxLoops = 10;
while($sQ->have_posts()) {
    if($loopCount == $maxLoops)
    {
        break;
    }
    $loopCount ++;
}

这应该只循环10次

答案 1 :(得分:0)

您需要计算一个纬度/长度位置与另一个纬度/长度位置的距离。计算此距离后,您可以将其与最大半径进行比较。一种算法是Haversine formula

您将能够在互联网上找到许多这样的实现,这是PHP中的实现。

function getDistance($latFrom, $longFrom, $latTo, $longTo)
{
    $dLat = deg2rad($latTo - $latFrom);
    $dLon = deg2rad($longTo - $longFrom);

    $angle = sin($dLat / 2) * sin($dLat / 2) + cos(deg2rad($latFrom)) * cos(deg2rad($latTo)) * sin($dLon / 2) * sin($dLon / 2);

    $c     = 2 * asin(sqrt($angle));

    $distance = 6371000 * $c;

    // Distance is in metres
    return $distance;
}

请参阅(上面的代码段和基本相同的代码段):How to check if a certain coordinates fall to another coordinates radius using PHP only

传递你的中心纬度/经度和记录纬度/经度以获得距离(以米为单位)然后进行比较:

if (getDistance(0, 0, 0, 0) < 1000) {
    // do stuff
}

1000是你的半径。