Home › Forums › Feature Requests › Google Map & City Name › Reply To: Google Map & City Name
I made here a shortcode to show the “city, country” from the coordinates
You have to change the get_field(‘adresse’, $id) to your ACF field name.
Here you need to write also the post id or you can just delete the beginning.
I m not sure if you need a google api key … i tried it both with and without and it works…
You can put that code in the functions.php of your theme
It could be that you have to change on your server in the php.ini ( allow_url_fopen=0 ….. to …. allow_url_fopen=1 )… when you get an error like this “http:// wrapper is disabled in the server configuration by allow_url_fopen=0” …
I m not sure… if this code is slowing a website also down. hm?
// [location id=""]
function location_shortcode( $atts ) {
extract( shortcode_atts(
array(
'id' => '',
), $atts )
);
if ( isset( $id ) && get_field('adresse', $id) ) {
$google_map = get_field('adresse', $id);
$url = 'https://maps.googleapis.com/maps/api/geocode/json?latlng='.$google_map['lat'].','.$google_map['lng'].'&sensor=true';
$data = @file_get_contents($url);
$jsondata = json_decode($data,true);
if(is_array($jsondata) && $jsondata['status'] == "OK")
{
// city
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("locality", $address["types"])) {
$city = $address["long_name"];
}
}
}
// country
foreach ($jsondata["results"] as $result) {
foreach ($result["address_components"] as $address) {
if (in_array("country", $address["types"])) {
$country = $address["long_name"];
}
}
}
}
$seperator = ', ';
$location = $city . $seperator . $country;
return $location;
}
}
add_shortcode( 'location', 'location_shortcode' );
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.