In this post you will see how I cobbled together information from other genius people to create a plotting of a commenter’s supplied geographic location. To do this I extended the comment field to accept location, built a new feed template formatted for GeoRSS and used the Google Maps V3 plug-in to create a short code.
I’m not going to tell you this is the BEST way to do it, or that there aren’t other ways to do it. But I will tell you that it gets the job done. Any suggestions on how to improve this process are much appreciated.
Here are the elements I used to achieve this. Let’s talk about them:
1. Comprehensive Google Map Plugin
Install and activate this plug-in. It touts itself as a simple and intuitive, yet elegant and fully documented Google map plugin that installs as a widget and a short code. No limited plugin editions or limited functionality! This is the full version of this free and premium plugin.
Get it: http://wordpress.org/extend/plugins/comprehensive-google-map-plugin/
2. Creating a geoRSS feed from WordPress
Take a look at this back-to-basics tutorial for generating a geoRSS feed from WordPress, correctly formatted for use in Google Maps KML layers.
View it here: http://www.strangerpixel.com/2011/03/creating-a-georss-feed-from-wordpress/
3. Search MySQL
If you aren’t so handy with the database search query syntax of WordPress, here’s something to look at. I didn’t use code from this directly, but it gave me an idea of how to write the query properly.
View it here: http://stackoverflow.com/questions/4443280/php-mysql-search-query
4. The Google Geocoding API for php
Geocoding is the process of converting addresses (like “1600 Amphitheatre Parkway, Mountain View, CA”) into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use to place markers or position the map. The Google Geocoding API provides a direct way to access a geocoder via an HTTP request. Additionally, the service allows you to perform the converse operation (turning coordinates into addresses); this process is known as “reverse geocoding.”
Find the quick API url for doing the job, plus MUCH more here: https://developers.google.com/maps/documentation/geocoding/
5. Putting it all together:
$id = '91'; //hacky way to pass the post id, as a GET will not work via the GeoRSS ingestion:
echo ‘‘; ?>
WHERE ID = ‘”.$id.”‘
post_title; ?> – Jamaica Plain, MA
SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_content
WHERE comment_post_ID = ‘”.$id.”‘
$count = count($comments) – 1;
for($i = 0; $i get_results($wpdb->prepare(”
SELECT meta_key, meta_value
WHERE meta_key = ‘_iti_ccf_post_location’
AND comment_id = ‘”.$comments[$i]->comment_ID.”‘
$countj = count($commentmeta) – 1;
for($j = 0; $j meta_value;
$address = explode(‘ ‘,$address);
$address = implode(‘%20′,$address);
$geocode=file_get_contents(‘http://maps.googleapis.com/maps/api/geocode/json?address=’.$address.’&sensor=true’); $output= json_decode($geocode);
$geoLat = $output->results->geometry->location->lat;
$geoLon = $output->results->geometry->location->lng;
$coords = $geoLat.’ ‘.$geoLon;
post_title; ?> – meta_value) ?>