开发者

How would you use Meta Value to mimic this Google Maps Query?

There is a wonderful example of using Google maps for a store locator here: http://code.google.com/apis/maps/articles/phpsqlsearch_v3.html

The example uses a simple table with 3 columns- address, latitude, longitude.

I'm trying to integrate this in wordpress using Custom Post type for each new location, and i've successfully been able to save the address, latitude, and longitude value as meta data along with each new custom post.

What i'm having a hard time doing is pulling this data/Query to convert to XML the same way that the google example provides.

It would be nice to just keep all the data inside WordPress custom posts without creating an entire seperate table to handle it.

Here is the standard example query:

// Search the rows in the markers table
$query = sprintf("SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
  m开发者_如何学Pythonysql_real_escape_string($center_lat),
  mysql_real_escape_string($center_lng),
  mysql_real_escape_string($center_lat),
  mysql_real_escape_string($radius));
$result = mysql_query($query);

How would you approach mimicking this to work within wordpress CPT meta data?


I've figured it out- this is the SQL Query:

SELECT  wp_posts.post_title as name, 
address.meta_value as address,
 latitude.meta_value as lat,
 longitude.meta_value as lng,
 telephone.meta_value as telephone,

   ( 3959 * acos(
    cos( radians( '%s' ) ) *
    cos( radians( CONVERT( latitude.meta_value, DECIMAL( 10, 6 ) ) ) ) *
    cos( radians( CONVERT( longitude.meta_value, DECIMAL( 10, 6 ) ) ) - radians( '%s' ) ) +
    sin( radians( '%s' ) ) * sin( radians( CONVERT( latitude.meta_value, DECIMAL( 10, 6 ) ) ) )
     ) ) AS distance

 FROM wp_posts
 LEFT JOIN wp_postmeta AS address ON(
 wp_posts.ID = address.post_id
 AND address.meta_key = '_dealer_address'
 )
 LEFT JOIN wp_postmeta AS latitude ON(
 wp_posts.ID = latitude.post_id
 AND latitude.meta_key = '_dealer_latitude'
 )
 LEFT JOIN wp_postmeta AS longitude ON(
 wp_posts.ID = longitude.post_id
 AND longitude.meta_key = '_dealer_longitude'
 )
 LEFT JOIN wp_postmeta AS telephone ON(
 wp_posts.ID = telephone.post_id
 AND telephone.meta_key = '_dealer_telephone'
 )
 WHERE wp_posts.post_type = 'dealers' HAVING distance < '%s' ORDER BY distance LIMIT 0 ,      20
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜