Created
October 28, 2019 12:50
-
-
Save sebastienserre/f356df46d9f048b48ab28d520d423323 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Post in relation https://thivinfo.com/blog/tutoriels/geocoder-une-adresse-sans-utiliser-google-maps/