YogeshChauhan . com

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.

amazon

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 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Kanban vs Scrum: The two frameworks of agile principlesMisc5 Steps to Create a Line using Canvas Tag in HTML5HTMLHow to create rotating texts using JavaScript and CSS?CSSSolution to “Call to undefined function mysql_error()” in RevSlider WordPress PluginWordPressContainers 101: What are containers?MiscList of WordPress directories functionsWordPress