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 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
Window setTimeout() Method in JavaScriptJavaScriptThe difference between isFinite() method and isFinite() function in JavaScriptJavaScriptA Quick Guide to Object-Oriented Programming in PHPPHPHow to define visibility for a property in PHP?PHPWhat does Containment mean in React?Reactclip and clip-path properties in CSSCSS