Welcome Guest, Not a member yet? Register   Sign In
Passing Results from SQL to Google Maps API in CodeIgniter
#6

[eluser]SitesByJoe[/eluser]
Here's what I've done: (I use the jmaps plugin for jquery FYI)

In my template view I have code like this in the head of the document: (inside my script tag)

Code:
<?php if ( $map_display == 'city' || $map_display == 'property') : ?>                            
    var url = '/map/coordinates.php';
    $.get(url, {'address': '<?=$address?>', 'key': '<?=$this->config->item('google_map_key')?>'}, function(data) {
            var json = eval("(" + data + ")");
            if (json.coordinates.lat != 0) {
                    $('#map').jmap({ center: [json.coordinates.lat, json.coordinates.lng], zoom: <?=$zoom_lvl?> });
            }
    });                
<?php else : ?>                
    var url = '/map/coordinates.php';
    var key = '<?=$this->config->item('google_map_key')?>';
    $('#map').jmap({ zoom: <?=$zoom_lvl?> });                    
<?php endif; ?>

The code that the url variable refers to is: (this is where the geocaching is done)

Code:
<?php
// this is a very unelegant method of getting map markers
// but it works for the moment - until more than 10000 happen in one day

header('Cache-Control: no-cache');
header('Pragma: no-cache');

// build the google url to ping for coordinates
$address = str_replace(' ', '+', $_GET['address']);
$key = $_GET['key'];
$url="http://maps.google.com/maps/geo?q=" . str_replace(" ", "+", $address) . "&output=csv&key;=" . $key;
// send the request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1);
$remote_content = curl_exec($curl);
curl_close ($curl);
// scrape the returned csv
$coordinates = split(',', $remote_content);
$lat = $coordinates[2];
$lng = $coordinates[3];
// return some json
$data = '
{"coordinates":
        {
            "lat": "' . $lat . '",
            "lng": "' . $lng . '",
            "address": "' . str_replace('+', ' ', $address) . '"
        }
}';
echo $data;

?>

As you can see this is just a basic PHP file, not a controller or anything. This is also an old solution, but it works well. If you're gather the same addresses repeatedly it'd be a good idea to database the coordinates to save your google requests from being too numerous.

Hope this helps!


Messages In This Thread
Passing Results from SQL to Google Maps API in CodeIgniter - by El Forum - 05-25-2010, 08:21 AM



Theme © iAndrew 2016 - Forum software by © MyBB