I’m trying to use meta data to get a postcode, so something like:
$property->add_meta_box(
'Property Info',
array(
array(
'name' => 'postcode',
'label' => 'Postcode',
'type' => 'text',
);
)
);
to create a meta box on every post type – property. How do I use this postcode to get a google map to generate on a page dynamically, IF the property has a postcode?
I’ll show how I did it but this was a year ago and there is perhaps a more updated, modern answer based on updates to Google Maps / Geocoding. Geocoding being the term for the process of converting a postcode to a Latitude/Longitude. This code was all in my
single.phpas I was using it to display the map on individual posts, you could do things differently depending on how you’ve set it up.I had originally used Yahoo Maps API to geocode a postcode and then use Google Maps for the map as Google Map’s geocoding was woefully inaccurate. Yahoo turned their service off, so I changed it to the Google Local Search version of geocoding which was more accurate at the time than Google Maps (this was last year, 2011).
Firs of all make sure you include the scripts to the map API in your
<head>https://developers.google.com/maps/documentation/javascript/tutorial#api_key
and
In your template you access your custom option (assuming it has an id of
postcode) like;And then use the following Javscript to geocode the address and generate your map (in
#map_canvas).Here’s the JS;
The HTML is as follows;