I'm under a 2-day crunch deadline (my own undoing, I know), and I need to implement a function in Wordpress that searches through a bunch of posts with custom content containing locations in a known field in wp_postmeta, (NOT geocoded) and shows a list of them based on proximity (i.e., within 20 miles) to a specific geocode.

I have no problem with the WP query - I'm just a little new to Google Map API, and the docs aren't terribly straightforward with regards to geocoding, which I ASSUME I have to do to determine proximity.

Additionally, I assume I'll just have to iterate through the list of each post and determine one at a time if they meet the proximity requirement.

I'm not asking for someone to do this for me, unless you w3ant to be paid to do it for me - I just need to be pointed in the right direction quickly. I can;t find ANYONE who knows how to do this - and I have some budget for it...

没有正确的解决方案

其他提示

I was researching for something similar a few days ago. How to handle a 4M geolocation record database with a fast SQL query.

And this page + this pdf helped.

Depends on what you store in your DB but iterating through each post every time will be slow. You need to offload the geolocation query to SQL level.

PS: I know link only responses are frowned upon...

Here are two starter scripts which you will need to modify

PHP

function buildGeocode($address_1, $address_2, $city, $state, $zip_code, $country){

        $base_url = "http://maps.googleapis.com/maps/api/geocode/json";

        $address = $address_1 . ' ' . $address_2 . ', ' . $city . ' ' . $state . ' ' . $zip_code . ' ' . $country;

        $request_url = $base_url . "?address=" . urlencode($address) . "&sensor=false";

        $data = json_decode(file_get_contents($request_url), true);

        return array('lat' => $data['results'][0]['geometry']['location']['lat'], 'lng' => $data['results'][0]['geometry']['location']['lng']);

}

MySQL

"SELECT SQL_CALC_FOUND_ROWS
            *
            FROM (SELECT location_tbl.*, ( 3959*(2*ASIN(SQRT(POW(SIN(((location_tbl.lat-" . $lat . ")*(PI()/180))/2),2)+COS(location_tbl.lat*0.017453293)*COS(" . $lat . "*0.017453293)*POW(SIN(((location_tbl.lng-" . $lng . ")*(PI()/180))/2),2)))) ) AS distance
                FROM location_tbl
            WHERE location_tbl.active = 1 AND location_tbl.deleted = 0) AS tmp_tbl
        WHERE distance <= '" . $dis . "'
        ORDER BY distance ASC"
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top