Yogesh Chauhan's Blog

How to add Google Static Map using ACF map field?

in WordPress on June 3, 2021

Here’s a complete guide on adding a map field and retrieving it:

How to add Google Map in WordPress using ACF?

Recently, Google has started charging for the dynamic maps and other APIs. Most (probably all) of the maps features were free in the past but now you have to pay for it.

The pricing guide is a bit complex and recently, few of our clients complained about Google billing and asked us if there is a way to fix it.

There is way to fix it if you want to just use iFrames and embed the Google map address. But you can’t have have design control in that way. Also, you can’t have control on the scale, zoom or markers either.

There is another solution though. Static Maps.

It’s not free but if you think your website(s) is going to make less than 100k Google Maps API requests then you should go for it cause it’s free upto 100k requests every month and Google also gives away $200 in credit for new users.

If you decide to use the Google Maps STatic API and you have ACF field already added to your website, here’s how you can get those fields values.

Get ACF map value in PHP and make a static map



$location = get_field('location');
echo '<img src="https://maps.googleapis.com/maps/api/staticmap?
center='.$location['lat'].','. $location['lng'].'
&#038;zoom=15
&#038;scale=2
&#038;size=900x320
&#038;maptype=roadmap
&#038;markers=size:mid|'.$location['address'].'
&#038;key=your_API_key" 
width="100%" 
height="auto"/>';


Get ACF map value in Laravel and make a static map



<img src="https://maps.googleapis.com/maps/api/staticmap?
center={{  get_field('location')['lat']}}, {{ get_field('location')['lng'] }}
&#038;zoom=15
&#038;scale=2
&#038;size=900x320
&#038;maptype=roadmap
&#038;markers=size:mid|{{ get_field('location')['address'] }}
&#038;key=your_API_key" 
width="100%" 
height="auto"/>


Read more about the Google Map fields used in the examples above.


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to add a new role in WordPress?WordPressaddcslashes() and addslashes() String Functions in PHPPHPWordPress: How to print ACF array field values?WordPressHow to control file extensions with an .htaccess file?MiscellaneousHow to send and receive query strings via links in Angular 9?AngularHow to add a Bar Chart in Angular App?Angular