Skip to content

Instantly share code, notes, and snippets.

@sebastienserre
Created October 28, 2019 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastienserre/f356df46d9f048b48ab28d520d423323 to your computer and use it in GitHub Desktop.
Save sebastienserre/f356df46d9f048b48ab28d520d423323 to your computer and use it in GitHub Desktop.
function get_lat_lng( $address ) {
$address = rawurlencode( $address );
$coord = get_transient( 'geocode_' . $address );
if( empty( $coord ) ) {
$url = 'http://nominatim.openstreetmap.org/?format=json&addressdetails=1&q=' . $address . '&format=json&limit=1';
$json = wp_remote_get( $url );
if ( 200 === (int) wp_remote_retrieve_response_code( $json ) ) {
$body = wp_remote_retrieve_body( $json );
$json = json_decode( $body, true );
}
$coord['lat'] = $json[0]['lat'];
$coord['long'] = $json[0]['lon'];
set_transient( 'geocode_' . $address, $coord, DAY_IN_SECONDS * 90 );
}
return $coord;
}
@sebastienserre
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment