Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

sebastienserre commented Oct 28, 2019

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