<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BIT Consultants &#187; Maps</title>
	<atom:link href="http://www.bitconsultants.net/tag/maps/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bitconsultants.net</link>
	<description></description>
	<lastBuildDate>Sat, 28 Jan 2012 02:19:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<item>
		<title>Geocoding with Google Maps and the Zend Framework</title>
		<link>http://www.bitconsultants.net/2010/geocoding-with-google-maps-and-the-zend-framework/</link>
		<comments>http://www.bitconsultants.net/2010/geocoding-with-google-maps-and-the-zend-framework/#comments</comments>
		<pubDate>Sat, 24 Apr 2010 17:51:40 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Code Snippets]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[Geocoding]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[Maps]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend Framework]]></category>

		<guid isPermaLink="false">http://www.bitconsultants.net/?p=231</guid>
		<description><![CDATA[Download Example Files: Zip RAR Maps are a great way to engage your users and visualize data, but it can be a little tricky to setup. Here<a href="http://www.bitconsultants.net/2010/geocoding-with-google-maps-and-the-zend-framework/"> Read More...</a>]]></description>
			<content:encoded><![CDATA[<h3><strong>Download Example Files</strong>:</h3>
<p> <a href="http://www.bitconsultants.net/examples/gmap.zip">Zip</a> <a href="http://www.bitconsultants.net/examples/gmap.rar">RAR</a><br />
<hr />
Maps are a great way to engage your users and visualize data, but it can be a little tricky to setup. Here I will walk you through the steps needed to get a map up and running.<br />
<script type="text/javascript">// <![CDATA[
 google_ad_client = "pub-4761320180230999"; /* 468x15, created 5/5/10 */ google_ad_slot = "9379654790"; google_ad_width = 468; google_ad_height = 15;
// ]]&gt;</script><br />
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">
</script></p>
<h3>Sign Up</h3>
<p>Apply for a Google Maps API Key (free) here using your domain without a subdomain or www (this will allow you to use key with or without www and on subdomains): <a href="http://code.google.com/apis/maps/signup.html" rel="nofollow">http://code.google.com/apis/maps/signup.html</a></p>
<h3>Configuration</h3>
<p>Put the API key in your configuration file (application.ini, config.ini, etc.) like this:</p>
<pre class="brush: php; title: Code Example; notranslate">
[google]
google_map_key =  YOUR_KEY
</pre>
<h3>Bootstrapping</h3>
<p><del datetime="2011-01-08T03:34:24+00:00">In your index.php file, add the following code:</del><br />
As a concerned user pointed out, that is terrible practice and was a remnant of my first tinkerings with ZF. You should place this code in your Bootstrap.php file:</p>
<pre class="brush: php; title: Code Example; notranslate">
protected function initGoogle(){
	defined('CONFIG_PATH') or define('CONFIG_PATH', APPLICATION_PATH . '/configs/application.ini');
	Zend_Registry::set('google',  new Zend_Config_Ini(CONFIG_PATH, 'google'));
}
</pre>
<p>This will add your api key to the registry so that it can be accessed throughout your application.</p>
<h3>Model</h3>
<p>In your models directory, create a new class called &#8216;Geocoder.php&#8217;. This will be the file that contains the application logic for retreiving coordinates on a given location.</p>
<p><strong>models/Geocoder.php</strong></p>
<pre class="brush: php; title: Code Example; notranslate">

class Geocoder{
    protected $key;

    public function __construct($api_key)
    {
        $this-&gt;key = $api_key;
    }

    public function _getGeocodedLatitudeAndLongitude($address)
    {
        $client = new Zend_Http_Client();
        $client-&gt;setUri($this-&gt;getGeocodingUri());
        $client-&gt;setParameterGet('q', urlencode($address))
               -&gt;setParameterGet('output', 'json')
               -&gt;setParameterGet('sensor', 'false')
               -&gt;setParameterGet('key', $this-&gt;key);

        $result = $client-&gt;request('GET');
        $response = Zend_Json_Decoder::decode($result-&gt;getBody(),
                    Zend_Json::TYPE_OBJECT);
        return $response;
    }

    public function getCoordinates($address)
    {
        $response = $this-&gt;_getGeocodedLatitudeAndLongitude($address);
        if(isset($response-&gt;Placemark[0]-&gt;Point-&gt;coordinates[1])){
             return array(
                'lat' =&gt; $response-&gt;Placemark[0]-&gt;Point-&gt;coordinates[1],
                'lon' =&gt; $response-&gt;Placemark[0]-&gt;Point-&gt;coordinates[0]
            );
        } else {
			return null;
		}
    }

    private function getGeocodingUri()
    {
        return 'http://maps.google.com/maps/geo';
    }
}
?&gt;
</pre>
<h3>Usage</h3>
<p>Alright, that was pretty easy. Now we just have to implement this in our controller. Here is a sample model that uses the Geocoder class to return geotagged User objects.<br />
<strong>models/User.php</strong></p>
<pre class="brush: php; title: Code Example; notranslate">
class User extends Zend_Db_Table_Abstract{
	protected $_name = 'users';
	protected $key;
	protected $geocoder;

	public function init()
	{
		$this-&gt;key = Zend_Registry::get('google')-&gt;google_map_key;
		$this-&gt;geocoder = new Geocoder($this-&gt;key);

	}
	public function getUsersAndGeocode()
	{
		$result = $this-&gt;fetchAll();

		$users    = $result-&gt;_toArray();
		foreach($users as $user)
		{
			$address = &quot;{$user['address']} {$user['city']} {$user['state']} {$user['zip']}&quot;;

			$latlon = $this-&gt;geocoder-&gt;getCoordinates($address);
			if($latlon)
			{
				$user['lat'] = $latlon['lat'];
				$user['lon'] = $latlon['lon'];
			}
		}
		return $users;
	}

}
?&gt;
</pre>
<p>The controller part is a little too easy. We will setup a reference to our user table and create a map action. Within the map action we get all of users and geocode them. We pass that collection to our view. This file should be located here:<br />
<strong>controllers/UserController.php</strong></p>
<pre class="brush: php; title: Code Example; notranslate">
class UserController extends Zend_Controller_Action{
	protected $user_table;
	public function init()
	{
		//database connectivity stuff is up to you

		$this-&gt;user_table = new User();
	}
	public function mapAction()
	{
		$this-&gt;view-&gt;api_key = Zend_Registry::get('google')-&gt;google_map_key;
		$users = $this-&gt;user_table-&gt;getUsersAndGeocode();
		$this-&gt;view-&gt;users = $users;
	}
}
?&gt;
</pre>
<p>And finally, the view. Here we will create our map div, load the Google Maps API (don’t forget to sub out your key in the script src). We are using a custom marker here (personIcon); you can add your own image here by changing the path to your image. I recommend the image be pretty small with a transparent background. Given our current setup, this file would be located at this path:<br />
<strong>views/scripts/user/map.phtml</strong></p>
<pre class="brush: php; title: Code Example; notranslate">
&lt;div id=&quot;map&quot;&gt;

&lt;noscript&gt;
&lt;center&gt;
&lt;strong&gt;To view the map feature, you need to have Javascript enabled. If you are unsure about how to turn Javascript support on, please&lt;/p&gt;
&lt;p&gt;read Google's instructions found here: &lt;a href=&quot;https://www.google.com/support/adsense/bin/answer.py?hl=en&amp;amp;answer=12654&quot; target=&quot;_blank&quot;&gt;LINK&lt;/a&gt;&lt;/strong&gt;
&lt;/center&gt;&lt;/p&gt;
&lt;p&gt;&lt;/noscript&gt;
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://maps.google.com/maps?file=api&amp;amp;amp;v=2&amp;amp;amp;sensor=false&amp;amp;amp;key=&lt;?=$this-&gt;api_key?&gt;&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
 //Sorry, I love jquery! If you don't use window.onload = function(){initialize();}
    $(document).ready(function(){
        initialize();
    });
var map;
var personIcon;
function initialize() {
    if(GBrowserIsCompatible){

        map = new google.maps.Map2(document.getElementById(&quot;map&quot;));
        map.setUIToDefault();
        map.setCenter(new google.maps.LatLng('48.858001709', '2.29460000992'), 4);

        personIcon            = new GIcon();

		/**
		* Change this marker to be a small transparent custom image
		*
		*/
        personIcon.image      = '/img/common/custom_marker.png';
        personIcon.shadow     = &quot;http://www.google.com/mapfiles/shadow50.png&quot;;
        personIcon.iconSize   = new GSize(32,32);
        personIcon.shadowSize = new GSize(37,34);

        personIcon.iconAnchor = new GPoint(9,34);
        personIcon.infoWindowAnchor = new GPoint(9,2);
    }
    addOverlays(map);
}

function personMarker(point, index, dto){
 //set our marker to be the custom icon we created
    markerOptions = {icon:personIcon};
    var marker = new GMarker(point, markerOptions);

/**
 * This is the content in the info bubble
 */
    GEvent.addListener(marker, 'click', function(){
       marker.openInfoWindowHtml(
            '&lt;h3&gt;&lt;a href=&quot;/profile/view/'+dto.user_id+'&quot;&gt;'+dto.user_first+' '+ dto.user_last+'&lt;/a&gt;&lt;/h3&gt;'+
            dto.user_address+' '+dto.user_city+' '+dto.user_state+' '+dto.user_zip
        );

    });
    return marker;
}
/*
* Here is where we loop through our users and create new markers for each

*/
function addOverlays(map){
    &lt;?php
  $geo = $this-&gt;users-&gt;_toArray();
        $size = sizeof($geo);

 ?&gt;
    &lt;? for($i = 0; $i &lt; $size; $i++): ?&gt;
                var latLon = new GLatLng(&lt;?php echo $geo[$i]['lat'];?&gt;, &lt;?php echo $geo[$i]['lon'];?&gt;);

                map.addOverlay(personMarker(latLon, &lt;?=$i?&gt;, &lt;?=Zend_Json::encode($geo[$i])?&gt;));
    &lt;?php endfor; ?&gt;
}
&lt;/script&gt;
</pre>
<p>Post a comment if you have any questions</p>]]></content:encoded>
			<wfw:commentRss>http://www.bitconsultants.net/2010/geocoding-with-google-maps-and-the-zend-framework/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using xcache
Page Caching using xcache
Object Caching 287/332 objects using memcached

Served from: www.bitconsultants.net @ 2012-02-05 16:03:52 -->
