Created
July 6, 2012 17:36
-
-
Save alettieri/3061525 to your computer and use it in GitHub Desktop.
WordPress Geocode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function business_save_post($post_id, $post){ | |
// verify if this is an auto save routine. | |
// If it is our form has not been submitted, so we dont want to do anything | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
// verify this came from the our screen and with proper authorization, | |
// because save_post can be triggered at other times | |
if ( isset( $_POST[ 'business_noncename' ] ) && !wp_verify_nonce( $_POST['business_noncename'], plugin_basename( __FILE__ ) ) ) | |
return; | |
$business_meta = array(); | |
if( 'business' == $post->post_type ){ | |
if ( !current_user_can( 'edit_page', $post_id ) ) | |
return; | |
}else{ | |
if ( !current_user_can( 'edit_post', $post_id ) ) | |
return; | |
} | |
//... removed for brevity ... | |
// | |
// Set the lat_lon coordinates. | |
// | |
update_coords( $business_meta ); | |
//... removed for brevity ... | |
} | |
/** | |
* Updates lat_lon coordinates based on address. | |
* | |
* @method update_coords | |
* @param {Array} business_meta pointer | |
*/ | |
function update_coords(&$business_meta){ | |
// | |
// Get the business_meta address | |
// | |
$address_col = $business_meta["business_address"]; | |
$street = $address_col["business_street"]; | |
$zip = $address_col["business_zip"]; | |
$city = $address_col["business_city"]; | |
$state = $address_col["business_state"]; | |
// | |
// Create the address query variable. | |
// | |
$address = sprintf( '%1$s, %2$s, %3$s %4$s', $street, $city, $state, $zip ); | |
// | |
// Let's get the lat_lon coordinates for this address. | |
// | |
$lat_lon = GeoCode::get_latlon( $address ); | |
// | |
// Did we get something back? | |
// | |
if( isset( $lat_lon ) && count( $lat_lon ) > 0 ) { | |
// | |
// Yes! Let's grab the lat_lon coordinates. | |
// | |
$business_meta["business_lat_lon"] = array( | |
"business_lat" => $lat_lon[ 0 ] | |
, "business_lon" => $lat_lon[ 1 ] | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if( ! class_exists("pvd_helpers") ) | |
include_once( "helpers.php" ); | |
/** | |
* GeoCode Helper object. | |
* | |
* Class GeoCode | |
*/ | |
class GeoCode { | |
/** | |
* Calls the google geocode api with the urlencoded address. | |
* If something goes wrong, the function returns false | |
* | |
* @method get_latlon | |
* @param {String} address 1600+Amphitheatre+Parkway,+Mountain+View,+CA | |
*/ | |
public function get_latlon( $address ) { | |
// | |
// Default return value. | |
// | |
$lat_lon = array(); | |
// | |
// Google Maps Geocode API url | |
// | |
$geo_url = sprintf( 'http://maps.googleapis.com/maps/api/geocode/json?address=%1$s&sensor=false', urlencode( $address ) ); | |
// | |
// Call the Google maps api. | |
// | |
$response = pvd_helpers::make_request( $geo_url ); | |
// | |
// Do we have a response? | |
// | |
if( $response ) { | |
// | |
// Yes, let's parse the json | |
// | |
try { | |
$geo_json = json_decode( $response ); | |
} catch( Exception $ex ) { | |
// | |
// json_decode failed, failed hard. | |
// | |
$geo_json = null; | |
} | |
// | |
// Ensure that we have a geographic data | |
// | |
if( isset( $geo_json ) && $geo_json->status == "OK" ){ | |
// | |
// Parse out the location property | |
// | |
$location = $geo_json->results[ 0 ]->geometry->location; | |
// | |
// Set the lat_lon array, our final return value! | |
// | |
$lat_lon = array( $location->lat, $location->lng ); | |
}// endif | |
}// endif | |
// | |
// Return the lat_lon array. | |
// This could also be an empty array. | |
// | |
return $lat_lon; | |
} //end get_latlon | |
} // end class GeoCode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if( !class_exists( 'WP_Http' ) ) | |
include_once( ABSPATH . WPINC. '/class-http.php' ); //include WP_Http | |
/** | |
* Helper Library | |
*/ | |
class pvd_helpers{ | |
/** | |
* make_request | |
* Utilizes WordPress' WP_Http Class to make http requests. | |
* | |
* @param {String} $url | |
* @return {Boolean or String} response | |
**/ | |
function make_request( $url ) { | |
// | |
// Initialize the WP_Http object. | |
// | |
$http_request = new WP_Http(); | |
// | |
// Initial Return value. | |
// | |
$return_data = false; | |
try { | |
$request = $http_request->request( $url ); | |
// | |
// Did we get a response? | |
// | |
if( $request && !isset( $request->errors ) ) | |
// | |
// Yes, let's grab the response body. | |
// | |
$return_data = $request[ "body" ]; | |
} catch( Exception $ex ) { | |
// | |
// Our request failed. | |
// Let's be sure to return false. | |
// Our code should be prepared to handle boolean responses. | |
// | |
$return_data = false; | |
} | |
// | |
// Our final response. | |
// Either a string or boolean:false (if it failed). | |
// | |
return $return_data; | |
} | |
} |
Hey Anca,
Cool, glad it worked for you! I am on vacation right now. Super bummed
about missing WordCamp! Have a blast!
Sent from my iOS device.
…On Aug 2, 2012, at 5:14 PM, Anca Mosoiu ***@***.*** wrote:
Hey Antonio,
Just wanted to thank you again for posting this. It worked!!! Hope to see you this weekend and WordCamp.
-- Anca.
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/3061525
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Antonio,
Just wanted to thank you again for posting this. It worked!!! Hope to see you this weekend and WordCamp.
-- Anca.