<?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>Uncle Tomm&#039;s blog</title>
	<atom:link href="http://www.tdmarketing.co.nz/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tdmarketing.co.nz/blog</link>
	<description>A place to record how I solved web and IT related stuff I scratched my head over</description>
	<lastBuildDate>Thu, 08 Mar 2012 22:10:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>HTML forms, PHP, MySQL and UTF-8 &#8211; solving the unnerving MS Word special characters problem</title>
		<link>http://www.tdmarketing.co.nz/blog/2012/03/08/html-forms-php-mysql-and-utf-8-solving-the-unnerving-ms-word-special-characters-problem/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2012/03/08/html-forms-php-mysql-and-utf-8-solving-the-unnerving-ms-word-special-characters-problem/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 23:14:47 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=163</guid>
		<description><![CDATA[Ever had the problem that you paste copy from a MS Word document into a webform, hit the save or submit button and the outcome somehow looks like this&#8230; �The quick brown fox � and so on and so forth� &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2012/03/08/html-forms-php-mysql-and-utf-8-solving-the-unnerving-ms-word-special-characters-problem/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Ever had the problem that you paste copy from a MS Word document into a webform, hit the save or submit button and the outcome somehow looks like this&#8230;</p>
<blockquote><p>�The quick brown fox � and so on and so forth�</p></blockquote>
<p>&#8230;when it&#8217;s supposed to look like this&#8230;</p>
<blockquote><p>“The quick brown fox – and so on and so forth”</p></blockquote>
<p>&#8230;? Yes, I am specifically talking about the fancy quitation makrs and the elongated dash or hyphen that MS word automatically uses.</p>
<p>Well, welcome to the crazy world of character encoding. Just search the web and you&#8217;ll come across gazillion of blog posts and forum entries. All have a set of suggestions and it seems to work for some people while it doesn&#8217;t or others. Obviously I had the problem as well, otherwise I wouldn&#8217;t write about it now. Now how did I finally manage to solve the issue?</p>
<h3>1. Set the correct character encoding on the page header</h3>
<p>That was one of the suggestion I read most often, simply because it is the most important rule to follow. The html page that is supposed to display text with non-standard characters needs to be told that it should use the UTF-8 unicode character encoding.</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&nbsp;</pre></div></div>

<p>Was my problem solved? Nope. Not yet.</p>
<h3>2. Set the correct character encoding for html forms</h3>
<p>The data that I was trying to display was actually extracted from a MySQL database which itself was populated through an HTML form. When I checked the database entries I realised that the data in there was already screwed up. So I suspected the HTML form to submit the data in an incorrect character encoding. So next step was to set the correct character encoding for the html forms submitting data to MySQL:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&nbsp;</pre></div></div>

<p>Note the bit at the end of the opening form tag, i.e. <strong>accept-charset=&#8221;utf-8&#8243;</strong>.</p>
<p>Was my problem solved? Nope. Not yet.</p>
<p>But when I sent the php file&#8217;s form data to itself and echoed the POST data, it displayed the special characters correctly. So the problem had something to do with the PHP/MySQL interface or MySQL itself.</p>
<h3>3. Pass the data from PHP to MySQL with the correct character encoding</h3>
<p>After reading lots of <a href="http://stackoverflow.com/search?q=php+mysql+special+characters&amp;submit=search">stackoverflow.com</a> posts I ended up adjusting my data handling PHP scripts (i.e. the script that adds the form data to the database) to set the character encoding straight after the script the database connection and <em>before</em> the database selection:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$link</span><span style="color: #339933;">=</span><span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'usr'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'pass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_set_charset</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'utf8'</span><span style="color: #339933;">,</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Was my problem solved? Nope. Not yet.</p>
<h3>4. Make sure the MySQL database &amp; tables are set to use utf8 and utf_unicode_ci</h3>
<p>It seems you can screw up a lot with the wrong settings in MySQL. First of all, you can chose the character encoding of your database as well as your individual table fields. On top of that, you then have to  chose the collation of the character set which, as far as I understand it, sets which alphabet will be used and which special characters it contains.</p>
<p>The biggest problem I had was that even though I used <strong>utf-8</strong> as main character encoding in both database and table field settings and used <strong>utf8_bin</strong> as the collation, simply because I didn&#8217;t know it any better. But the copy was still stored in the messed up state. And that&#8217;s where I started to pull my hair out. I read tons of posts but they more or less all told me to do what I had done already (see points 1-3). Okay, after I while I noticed that it might be the collation that causes the problem and I set everythin to <strong>utf8_general_ci</strong>, simply because everybody and their dogs seemed to use it.</p>
<p>But it did not solve my problem.</p>
<p>Finally, I stumbled accross <a href="http://forums.mysql.com/read.php?103,187048,188748#msg-188748" target="_blank">this post</a> which to me suggested that the collation <strong>uft8_unicode_ci</strong> supports more special characters than <strong>utf8_general_ci</strong> (at the cost of performance). At that stage I couldn&#8217;t care less about performance. I just wanted the blood MS word quotation marks and hypens to render. So I changed database and table field settings to</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">character encoding: utf8
<span style="color: #000099;">collation</span>:          utf8_unicode_ci</pre></div></div>

<p>And finally, the stuff ended up in the database just the way MS word threw it in the clipboard. That was it. Simple enough&#8230; once you know it.</p>
<p>P.S. I probably should point out that leaving out any of the above 4 points results in garbled text again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2012/03/08/html-forms-php-mysql-and-utf-8-solving-the-unnerving-ms-word-special-characters-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing the GeoIP extension on Cent OS 5 running WHM/cPanel</title>
		<link>http://www.tdmarketing.co.nz/blog/2011/07/12/installing-the-geoip-extension-on-cent-os-5-running-whmcpanel/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2011/07/12/installing-the-geoip-extension-on-cent-os-5-running-whmcpanel/#comments</comments>
		<pubDate>Mon, 11 Jul 2011 23:39:03 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Server maintenance]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=148</guid>
		<description><![CDATA[We&#8217;re running a managed dedicated server hosted by Liquidweb. Those guys are usually a pretty good bunch, but sometimes it shows that they are also just mere mortals. Last weekend I asked them to upgrade MySQL from version 4 to &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2011/07/12/installing-the-geoip-extension-on-cent-os-5-running-whmcpanel/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re running a managed dedicated server hosted by <a href="http://www.liquidweb.com">Liquidweb</a>. Those guys are usually a pretty good bunch, but sometimes it shows that they are also just mere mortals. Last weekend I asked them to upgrade MySQL from version 4 to 5 and I also casually asked whether our server OS could be upgraded from Cent OS 4 to 5. The weekend was coming up so it seemed like a good idea to get that underway.</p>
<p>Well, it turned out they actually had to upgrade the OS in order to get MySQL 5 to work. So the server was reinstalled, all hosting accounts re-imaged etc. Unfortunately, none of the PHP/server extensions &#8211; such as ImageMagick and GeoIP &#8211; survived the upgrade. No sweat, I thought, logged into our WHM site and tried to install the missing modules there. But the PECL installation always failed. So I contacted Liquidweb again &#8211; but for the first time ever I did not hear back from them for more than an hour (as I said, they&#8217;re usually pretty good, but I guess even they need a break every now and then).</p>
<p>It was kind of unnerving that some of our websites were not working because the extensions were missing. So I took it upon myself to install the stuff with the aid of Google and my moderate Linux knowledge. While imagemagick proved to be a piece of cake following directions given <a href="http://www.matteomattei.com/en/install-imagemagick-with-php-imagick-extension-on-centos/" target="_blank">here</a>, GeoIP was a somewhat tougher nut to crack. That was because the installation instructions provided <a href="http://benlancaster.wordpress.com/2009/06/08/installing-mod_geoip-on-centos-5-x-or-rhel/" target="_blank">here</a> did not work for me as our yum repositories did not know anything about GeoIP. But at least I knew which packages were necessary:</p>
<pre>GeoIP
GeoIP-devel
GeoIP-data</pre>
<p>Since the server was running Cent OS 5 now, I needed the correct rpms. God was I glad that a site like <a href="http://rpm.pbone.net/" target="_blank">http://rpm.pbone.net/</a> exists! Here I found everything I needed (careful &#8211; watch for the correct/corresponding package versions!):</p>
<pre><a>http://rpm.pbone.net/index.php3/stat/4/idpl/14025435/dir/</a><a href="http://rpm.pbone.net/index.php3/stat/4/idpl/15707308/dir/centos_5/com/GeoIP-devel-1.4.5-1.el5.centos.x86_64.rpm.html" target="_blank">centos_5</a><a>/com/GeoIP-1.4.5-1.el5.centos.x86_64.rpm.html</a>
<a href="http://rpm.pbone.net/index.php3/stat/4/idpl/15707308/dir/centos_5/com/GeoIP-devel-1.4.5-1.el5.centos.x86_64.rpm.html" target="_blank">http://rpm.pbone.net/index.php3/stat/4/idpl/15707308/dir/centos_5/com/GeoIP-devel-1.4.5-1.el5.centos.x86_64.rpm.html</a>
<a href="http://rpm.pbone.net/index.php3/stat/4/idpl/15706982/dir/centos_5/com/GeoIP-data-20090201-1.el5.centos.i386.rpm.html" target="_blank">http://rpm.pbone.net/index.php3/stat/4/idpl/15706982/dir/centos_5/com/GeoIP-data-20090201-1.el5.centos.i386.rpm.html</a></pre>
<p>I logged into our server via SSH and navigated to the /root/tmp/ folder where I could safely execute the installation of the packages. I downloaded the three rpms from one of the mirrors:</p>
<pre>wget ftp://ftp.muug.mb.ca/mirror/centos/5.6/extras/x86_64/RPMS/GeoIP-1.4.5-1.el5.centos.x86_64.rpm
wget ftp://ftp.muug.mb.ca/mirror/centos/5.6/extras/x86_64/RPMS/GeoIP-devel-1.4.5-1.el5.centos.x86_64.rpm
wget ftp://ftp.muug.mb.ca/mirror/centos/5.6/extras/i386/RPMS/GeoIP-data-20090201-1.el5.centos.i386.rpm</pre>
<p>And installed them one by one:</p>
<pre>rpm -ivh GeoIP-1.4.5-1.el5.centos.x86_64.rpm
rpm -ivh GeoIP-devel-1.4.5-1.el5.centos.x86_64.rpm
rpm -ivh GeoIP-data-20090201-1.el5.centos.i386.rpm</pre>
<p>With sweaty palms I then typed the command</p>
<pre>pecl install geoip</pre>
<p>and jumped for joy when I did not get any error messages! I restarted apache via the WHM panel and &#8211; woohoo! &#8211; GeoIP extension worked as it should!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2011/07/12/installing-the-geoip-extension-on-cent-os-5-running-whmcpanel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL remote user access not working on Ubuntu + Plesk</title>
		<link>http://www.tdmarketing.co.nz/blog/2011/06/27/mysql-remote-user-access-not-working-on-ubuntu-plesk/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2011/06/27/mysql-remote-user-access-not-working-on-ubuntu-plesk/#comments</comments>
		<pubDate>Sun, 26 Jun 2011 23:46:33 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Networking]]></category>
		<category><![CDATA[Server maintenance]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=135</guid>
		<description><![CDATA[Last week one of my mate&#8217;s private vServer was hacked, hijacked and used for DoC attacks which prompted his provider (Strato) to disable the machine and recommended a reinstallation of the OS. That was not as bad as it sounds, &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2011/06/27/mysql-remote-user-access-not-working-on-ubuntu-plesk/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Last week one of my mate&#8217;s private vServer was hacked, hijacked and used for DoC attacks which prompted his provider (Strato) to disable the machine and recommended a reinstallation of the OS. That was not as bad as it sounds, as all data on the server were on backup and the OS was outdated to boot (OpenSuse 9!).</p>
<p>The vServer was reinstalled with Ubuntu 10.04 LTS and Plesk 10. After all data were back in place, all sites back up and running, all that was missing was the remote connection to the MySQL server. My mate uses Navicat (as do I, btw) to remotely manage his databases. So a new user was added to MySQL with remote access rights, the Navicat settings were altered accordingly and&#8230; a connection could not be established. D&#8217;Oh!</p>
<p>What went wrong? Well, I had a look at the issue. First off, I checked the <strong>[mysqld]</strong> settings in the MySQL configuration (aka <strong>my.cnf</strong>).</p>
<pre>user            = mysql
socket          = /var/run/mysqld/mysqld.sock
port            = 3306
bind-address    = 0.0.0.0
basedir         = /usr
datadir         = /var/lib/mysql
tmpdir          = /tmp
skip-external-locking</pre>
<p>That looked all good. The standard port was set to 3306, the bind-address was set to listen to all incoming traffic (although a security related &#8220;no-no&#8221; in my opinion), and no trace of the &#8220;skip-networking&#8221; directive that might have negated the bind-address settings.</p>
<p>Just to be on the safe side I restarted the mysql server (<strong>service restart mysql</strong>), but Navicat would still not connect.</p>
<p>Next stop was to have a look at the output generated by <strong>iptables -L</strong> and there I found this:</p>
<pre>DROP       tcp  --  anywhere             anywhere            tcp dpt:mysql</pre>
<p>Hmm. It seems that tcp connections to MySQL are dropped for some reason. Probably not a bad idea to keep that setting (and set my.cnf&#8217;s bind-address to 127.0.0.1) just to avoid new system intrusions via the MySQL route.</p>
<p>Of course that meant that the Navicat connection problem remained&#8230; that is, as far as a direct connection to the MySQL server is concerned. However, Navicat permits to connect to MySQL via SSH tunneling*. And using that option works like a treat minus the security risks of an open MySQL server.</p>
<p>* A bit confusing is that you have to manually set the MySQL Server address to &#8220;localhost&#8221; in Navicat&#8217;s main connection property window. I would have thought that the software would choose this setting automatically, if SSH tunneling is activated. Well, just a minor nuisance.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2011/06/27/mysql-remote-user-access-not-working-on-ubuntu-plesk/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Post to Twitter via PHP script or twitterfeed</title>
		<link>http://www.tdmarketing.co.nz/blog/2011/06/10/post-to-twitter-via-php-script-or-twitterfeed/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2011/06/10/post-to-twitter-via-php-script-or-twitterfeed/#comments</comments>
		<pubDate>Fri, 10 Jun 2011 04:11:14 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=120</guid>
		<description><![CDATA[Oh how difficult it can be to find an easy solutions. After probing the net for ways how to post stuff to Twitter I finally came across a blog post that was not outdated and is still working. Lest I &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2011/06/10/post-to-twitter-via-php-script-or-twitterfeed/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Oh how difficult it can be to find an easy solutions. After probing the net for ways how to post stuff to Twitter I finally came across a blog post that was not outdated and is still working. Lest I never forget (until Twitter changes their authentication protocol again)!</p>
<p><a href="http://tips4php.net/2010/12/twitter-oauth-the-easy-way-simple-post-to-twitter-script/">http://tips4php.net/2010/12/twitter-oauth-the-easy-way-simple-post-to-twitter-script/</a></p>
<p>However, the above method does not provide an oauth solution if you want to post to Twitter accounts other than your personal one. To post a news feed to a client&#8217;s twitter account requires them to complete the &#8220;Get all Oauth key&#8221; quests in Twitterland and that can be too much of a grind. But fortunately there is</p>
<p><a href="http://twitterfeed.com/">http://twitterfeed.com/</a></p>
<p>The good folks there provide a pretty painless Twitter/Facebook integration as long as they can tap into an RSS feed. Oh, well, and there is the tiny catch. To use bit.ly&#8217;s URL shortening service, twitterfeed users have to register and obtain a bit.ly API key. But that is reasonably straight forward and easy to explain:</p>
<blockquote><p>1. Go to <a href="http://bit.ly/">http://bit.ly</a> and sign up for a new account<br />
2. Go to <a href="http://bit.ly/a/your_api_key">http://bit.ly/a/your_api_key</a><br />
3. Copy and paste both keys into the corresponding fields of the     twitterfeed advanced settings</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2011/06/10/post-to-twitter-via-php-script-or-twitterfeed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL and the mystery of wrong numbers&#8230; always check your numeric types!</title>
		<link>http://www.tdmarketing.co.nz/blog/2011/05/20/mysql-and-the-mystery-of-wrong-numbers-always-check-your-numeric-types/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2011/05/20/mysql-and-the-mystery-of-wrong-numbers-always-check-your-numeric-types/#comments</comments>
		<pubDate>Fri, 20 May 2011 04:35:30 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=109</guid>
		<description><![CDATA[Doh! Another half hour wasted! I just spend the better part of half an hour searching for a bug in my PHP scripts that update a MySQL database via an AJAX call. One of the fields in the database table &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2011/05/20/mysql-and-the-mystery-of-wrong-numbers-always-check-your-numeric-types/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Doh! Another half hour wasted!</p>
<p>I just spend the better part of half an hour searching for a bug in my PHP scripts that update a MySQL database via an AJAX call. One of the fields in the database table is numeric, integer to be precise. When I tried to update the field with the value 228, the database would always change the value to 127. Why? All the other queries worked correctly. The field happily accepted a 48, a 85 and a 126. But the 228 always turned out as a 127.</p>
<p>Of course, it&#8217;s a late Friday afternoon and technically I should be doing more mundane things like writing a blog post, doing my timesheets or having a beer or two. But instead I was stuck with this issue. Just fix this issue and the scripts were good to go! Just this one tiny problem. Must be a typo somewhere in the PHP script. Or is jQuery somehow tinkering with the input field value before the form data is being sent? What could it be? What, what, what?!?</p>
<p>Then, all of a sudden, something made click and I couldn&#8217;t avoid a solid facepalm. I checked the database setup and found that the numeric type for the field was set to &#8220;tinyint&#8221; instead of &#8220;smallint&#8221;. And as we all should know unsigned tinyint&#8217;s maximum value is 127. Everything greater than that will be &#8220;rounded&#8221; to 127.</p>
<p>Now the field type is correctly set to &#8220;smallint&#8221; and my script is working. I just wish I could get that wasted half hour back and use it for mundane things like writing a blog post about those little blackouts that often cost too much time.</p>
<p>Oh well, time for a beer&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2011/05/20/mysql-and-the-mystery-of-wrong-numbers-always-check-your-numeric-types/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>google.maps.LatLng spits out NaN</title>
		<link>http://www.tdmarketing.co.nz/blog/2011/03/11/google-maps-latlng-spits-out-nan/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2011/03/11/google-maps-latlng-spits-out-nan/#comments</comments>
		<pubDate>Thu, 10 Mar 2011 22:04:41 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=82</guid>
		<description><![CDATA[Well, this is one of those examples that shows it pays off to sleep a night over certain problems. Yesterday afternoon I scratched my head over the Google Maps V3 API&#8217;s behaviour. All I wanted to do is change the &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2011/03/11/google-maps-latlng-spits-out-nan/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Well, this is one of those examples that shows it pays off to sleep a night over certain problems.</p>
<p>Yesterday afternoon I scratched my head over the Google Maps V3 API&#8217;s behaviour. All I wanted to do is change the zoom level of a map using the <code>fitBounds()</code> method. The latitude and longitude values were coming from a jQuery Ajax request that returned an JSON object. The object looked something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;bounds&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #3366CC;">&quot;sw&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;-45.99579575,170.05493469&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;ne&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;-45.48679779,170.77724915&quot;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I tried the following script to set the new bounds:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">parseJSON</span><span style="color: #009900;">&#40;</span>ajaxResponse<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sw <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">bounds</span>.<span style="color: #660066;">sw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ne <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span>obj.<span style="color: #660066;">bounds</span>.<span style="color: #660066;">ne</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
newbounds <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLngBounds</span><span style="color: #009900;">&#40;</span>sw<span style="color: #339933;">,</span>ne<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, the result was that it completely caked the Map. The map literally zoomed (or panned, who knows?) to uncharted territory after which any interactions with it would not result in any updates. The map had effectively crashed.</p>
<p>WTF, why? I asked myself. So I checked what values google.maps.LatLng were producing. NaN. Not numbers obviously, but why? I chewed on that for half an hour before I decided to leave it for the day.</p>
<p>Now this morning I had a look at it again and it quickly dawned on me&#8230; the JSON data was delivered as a string but the google.map.LatLng class expects floats. So I altered the script accordingly:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">parseJSON</span><span style="color: #009900;">&#40;</span>ajaxResponse<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> sw<span style="color: #339933;">=</span>obj.<span style="color: #660066;">bounds</span>.<span style="color: #660066;">sw</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// split string to allow parseFloat of data</span>
<span style="color: #003366; font-weight: bold;">var</span> ne<span style="color: #339933;">=</span>obj.<span style="color: #660066;">bounds</span>.<span style="color: #660066;">ne</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">','</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
sw <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span>parseFloat<span style="color: #009900;">&#40;</span>sw<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>parseFloat<span style="color: #009900;">&#40;</span>sw<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
ne <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span>parseFloat<span style="color: #009900;">&#40;</span>ne<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>parseFloat<span style="color: #009900;">&#40;</span>ne<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
newbounds <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLngBounds</span><span style="color: #009900;">&#40;</span>sw<span style="color: #339933;">,</span>ne<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
map.<span style="color: #660066;">fitBounds</span><span style="color: #009900;">&#40;</span>newbounds<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And lo and behold&#8230; it works.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2011/03/11/google-maps-latlng-spits-out-nan/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create marker with custom labels in Google Maps API v3</title>
		<link>http://www.tdmarketing.co.nz/blog/2011/03/09/create-marker-with-custom-labels-in-google-maps-api-v3/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2011/03/09/create-marker-with-custom-labels-in-google-maps-api-v3/#comments</comments>
		<pubDate>Tue, 08 Mar 2011 21:19:54 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=54</guid>
		<description><![CDATA[Took me quite some time to get to the bottom of how to put custom labels on markers (aka &#8220;placemarks&#8221;) in Google Maps API v3. I must admit that I am quite puzzled as to why Google hasn&#8217;t built in &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2011/03/09/create-marker-with-custom-labels-in-google-maps-api-v3/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>Took me quite some time to get to the bottom of how to put custom labels on markers (aka &#8220;placemarks&#8221;) in Google Maps API v3.</p>
<p>I must admit that I am quite puzzled as to why Google hasn&#8217;t built in this functionality in their google.Maps.Marker class. You can do a lot with that class like define a title via setTitle (i.e. creating a mouseover-tooltip for markers). Why a setLabel feature is missing&#8230; I don&#8217;t know. Instead it is necessary to write your own class &#8211; or at the very least search for a solution on some developers blog.</p>
<p>Now, finding a code snippet to create marker labels turned out to be more complicated than I thought. I always ended up on pages that dealt with <a title="MarkerClusterer" href="http://googlegeodevelopers.blogspot.com/2009/04/markerclusterer-solution-to-too-many.html" target="_blank">MarkerClusterer</a>, which primarily focusses on creating, well, marker clusters in maps that hold lots of markers. At first I tried to dig through the source code of MarkerClusterer to see how the marker labels were done. But I gave up when I realised that I had stared at the code for an hour without making any progress.</p>
<p>So finally I came across Marc Ridey&#8217;s blog post <a title="Marc Ridey's blog entry" href="http://blog.mridey.com/2009/09/label-overlay-example-for-google-maps.html" target="_blank">&#8220;Label overlay example for Google Maps API v3&#8243;</a>. The post has the solution I was looking for, albeit in a bit of a chopped up form as some of the info required to understand what he&#8217;s done is actually explained somewhere in the depths of the extensive comments section. Some vital info about how to manipulate the zIndex of the label (to have it on top of the marker) and setting the custom label are also only found in the comments. Furthemore, the code snippet wasn&#8217;t styled up which made it a bit tricky to follow the code in the context of the blog post.</p>
<p>After some fiddling I came up with a version that suited my needs &#8211; i.e. place marker labels on top of each marker (rather then below) and have a customizable text (rather than the automatic label generation from the markers geographic position. So here it is, the slightly altered Label class:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Define the overlay, derived from google.maps.OverlayView</span>
<span style="color: #003366; font-weight: bold;">function</span> Label<span style="color: #009900;">&#40;</span>opt_options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #006600; font-style: italic;">// Initialization</span>
     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">setValues</span><span style="color: #009900;">&#40;</span>opt_options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #006600; font-style: italic;">// Here go the label styles</span>
     <span style="color: #003366; font-weight: bold;">var</span> span <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">span_</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'span'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     span.<span style="color: #660066;">style</span>.<span style="color: #660066;">cssText</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'position: relative; left: -50%; top: -10px; '</span> <span style="color: #339933;">+</span>
                          <span style="color: #3366CC;">'white-space: nowrap;color:#ffffff;'</span> <span style="color: #339933;">+</span>
                          <span style="color: #3366CC;">'padding: 2px;font-family: Arial; font-weight: bold;'</span> <span style="color: #339933;">+</span>
                          <span style="color: #3366CC;">'font-size: 12px;'</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">div_</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     div.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>span<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     div.<span style="color: #660066;">style</span>.<span style="color: #660066;">cssText</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'position: absolute; display: none'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
Label.<span style="color: #660066;">prototype</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">OverlayView</span><span style="color: #339933;">;</span>
&nbsp;
Label.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">onAdd</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> pane <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getPanes</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">overlayImage</span><span style="color: #339933;">;</span>
     pane.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">div_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #006600; font-style: italic;">// Ensures the label is redrawn if the text or position is changed.</span>
     <span style="color: #003366; font-weight: bold;">var</span> me <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">listeners_</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
          google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'position_changed'</span><span style="color: #339933;">,</span>
               <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> me.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'text_changed'</span><span style="color: #339933;">,</span>
               <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> me.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">event</span>.<span style="color: #660066;">addListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'zindex_changed'</span><span style="color: #339933;">,</span>
               <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> me.<span style="color: #660066;">draw</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Implement onRemove</span>
Label.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">onRemove</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">div_</span>.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">removeChild</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">div_</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #006600; font-style: italic;">// Label is removed from the map, stop updating its position/text.</span>
     <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> I <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">listeners_</span>.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> I<span style="color: #339933;">;</span> <span style="color: #339933;">++</span>i<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">event</span>.<span style="color: #660066;">removeListener</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">listeners_</span><span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Implement draw</span>
Label.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">draw</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> projection <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getProjection</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> position <span style="color: #339933;">=</span> projection.<span style="color: #660066;">fromLatLngToDivPixel</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'position'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> div <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">div_</span><span style="color: #339933;">;</span>
     div.<span style="color: #660066;">style</span>.<span style="color: #660066;">left</span> <span style="color: #339933;">=</span> position.<span style="color: #660066;">x</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
     div.<span style="color: #660066;">style</span>.<span style="color: #660066;">top</span> <span style="color: #339933;">=</span> position.<span style="color: #660066;">y</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'px'</span><span style="color: #339933;">;</span>
     div.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'block'</span><span style="color: #339933;">;</span>
     div.<span style="color: #660066;">style</span>.<span style="color: #660066;">zIndex</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'zIndex'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//ALLOW LABEL TO OVERLAY MARKER</span>
     <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">span_</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To print labels I used the setMarker functions from the Google Map documentation example &#8220;<a title="Google Maps Javascript API V3 Overlays - Google Maps JavaScript API V3 - Google Code" href="http://code.google.com/apis/maps/documentation/javascript/overlays.html#ComplexIcons" target="_blank">Complex Icons</a>&#8221; with the added label functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> beaches <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span>
     <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Bondi Beach'</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">33.890542</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">151.274856</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Coogee Beach'</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">33.923036</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">151.259052</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Cronulla Beach'</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">34.028249</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">151.157507</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Manly Beach'</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">33.80010128657071</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">151.28747820854187</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'Maroubra Beach'</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">33.950198</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">151.259302</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> setMarkers<span style="color: #009900;">&#40;</span>map<span style="color: #339933;">,</span> locations<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> image <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">MarkerImage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'images/beachflag.png'</span><span style="color: #339933;">,</span>
     <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Size</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
     <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
     <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> shadow <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">MarkerImage</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'images/beachflag_shadow.png'</span><span style="color: #339933;">,</span>
          <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Size</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">37</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
          <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Point</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">32</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #003366; font-weight: bold;">var</span> shape <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
          coord<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">18</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">20</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">18</span> <span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
          type<span style="color: #339933;">:</span> <span style="color: #3366CC;">'poly'</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
     <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;</span> locations.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #003366; font-weight: bold;">var</span> beach <span style="color: #339933;">=</span> locations<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> myLatLng <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span><span style="color: #009900;">&#40;</span>beach<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> beach<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> marker <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">Marker</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
               position<span style="color: #339933;">:</span> myLatLng<span style="color: #339933;">,</span>
               map<span style="color: #339933;">:</span> map<span style="color: #339933;">,</span>
               shadow<span style="color: #339933;">:</span> shadow<span style="color: #339933;">,</span>
               icon<span style="color: #339933;">:</span> image<span style="color: #339933;">,</span>
               shape<span style="color: #339933;">:</span> shape<span style="color: #339933;">,</span>
               title<span style="color: #339933;">:</span> beach<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
              zIndex<span style="color: #339933;">:</span> beach<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #003366; font-weight: bold;">var</span> label <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Label<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
               map<span style="color: #339933;">:</span> map
          <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          label.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'zIndex'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1234</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          label.<span style="color: #660066;">bindTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'position'</span><span style="color: #339933;">,</span> marker<span style="color: #339933;">,</span> <span style="color: #3366CC;">'position'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          label.<span style="color: #660066;">set</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'text'</span><span style="color: #339933;">,</span> beach<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #006600; font-style: italic;">//label.bindTo('text', marker, 'position');</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>A working example of above code can be found <a href="http://www.tdmarketing.co.nz/google-maps-v3-labels.html" target="_blank">here</a>.</p>
<p>Of course, there&#8217;s also an alternative method:</p>
<p><a href="http://www.youtube.com/watch?v=I9TtDecveCE">Google Maps only using paper and scissors !</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2011/03/09/create-marker-with-custom-labels-in-google-maps-api-v3/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Reverse Geocoding via the Open Street Map and Google Maps APIs</title>
		<link>http://www.tdmarketing.co.nz/blog/2011/03/04/reverse-geocoding-via-the-open-street-map-and-google-maps-apis/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2011/03/04/reverse-geocoding-via-the-open-street-map-and-google-maps-apis/#comments</comments>
		<pubDate>Fri, 04 Mar 2011 03:04:03 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Open Street Map]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=18</guid>
		<description><![CDATA[How do you get the corresponding street address details for a geographic location expressed as latitude and longitude data only? Oh, and it should be a server side procdure too (i.e. no JavaScript). Well, here a a couple of solutions &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2011/03/04/reverse-geocoding-via-the-open-street-map-and-google-maps-apis/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>How do you get the corresponding street address details for a geographic location expressed as latitude and longitude data only? Oh, and it should be a server side procdure too (i.e. no JavaScript). Well, here a a couple of solutions for PHP.</p>
<h3>1. Google Maps</h3>
<p>The following HTTP GET request&#8230;</p>
<pre class="qoate-code">http://maps.google.com/maps/geo?q=37.34,-121.94&amp;output=json&amp;sensor=false</pre>
<p>&#8230;spits out a JSON response from the Google servers:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;name&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;37.34,-121.94&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;Status&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #3366CC;">&quot;code&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">200</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;request&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;geocode&quot;</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;Placemark&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #3366CC;">&quot;id&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;p1&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;address&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1060 Highland Ct, Santa Clara, CA 95050, USA&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;AddressDetails&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #3366CC;">&quot;Accuracy&quot;</span> <span style="color: #339933;">:</span> <span style="color: #CC0000;">8</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;Country&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #3366CC;">&quot;AdministrativeArea&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                         <span style="color: #3366CC;">&quot;AdministrativeAreaName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;CA&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #3366CC;">&quot;SubAdministrativeArea&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                              <span style="color: #3366CC;">&quot;Locality&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                   <span style="color: #3366CC;">&quot;LocalityName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Santa Clara&quot;</span><span style="color: #339933;">,</span>
                                   <span style="color: #3366CC;">&quot;PostalCode&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                        <span style="color: #3366CC;">&quot;PostalCodeNumber&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;95050&quot;</span>
                                   <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                                   <span style="color: #3366CC;">&quot;Thoroughfare&quot;</span> <span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                                        <span style="color: #3366CC;">&quot;ThoroughfareName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;1060 Highland Ct&quot;</span>
                                   <span style="color: #009900;">&#125;</span>
                              <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                              <span style="color: #3366CC;">&quot;SubAdministrativeAreaName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Santa Clara&quot;</span>
                              <span style="color: #009900;">&#125;</span>
                         <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                         <span style="color: #3366CC;">&quot;CountryName&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;USA&quot;</span><span style="color: #339933;">,</span>
                         <span style="color: #3366CC;">&quot;CountryNameCode&quot;</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;US&quot;</span>
                   <span style="color: #009900;">&#125;</span>
               <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
               <span style="color: #3366CC;">&quot;ExtendedData&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #3366CC;">&quot;LatLonBox&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #3366CC;">&quot;north&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">37.3431416</span><span style="color: #339933;">,</span>
                    <span style="color: #3366CC;">&quot;south&quot;</span><span style="color: #339933;">:</span> <span style="color: #CC0000;">37.3368464</span><span style="color: #339933;">,</span>
                    <span style="color: #3366CC;">&quot;east&quot;</span><span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">121.9368374</span><span style="color: #339933;">,</span>
                    <span style="color: #3366CC;">&quot;west&quot;</span><span style="color: #339933;">:</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">121.9431326</span>
               <span style="color: #009900;">&#125;</span>
          <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;Point&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #3366CC;">&quot;coordinates&quot;</span><span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">121.9399850</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">37.3399940</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">0</span> <span style="color: #009900;">&#93;</span>
          <span style="color: #009900;">&#125;</span>
     <span style="color: #009900;">&#125;</span> <span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Accessing the JSON info via PHP is fairly easy:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://maps.google.com/maps/geo?q=37.34,-121.94&amp;amp;output=json&amp;amp;sensor=false'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">//read the HTTP request</span>
<span style="color: #000088;">$jsondata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;">//parse the JSOPN response</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jsondata</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$jsondata</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Status'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'code'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #cc66cc;">200</span><span style="color: #009900;">&#41;</span>    <span style="color: #666666; font-style: italic;">//check data</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$addr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$jsondata</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'Placemark'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'address'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$addr</span><span style="color: #339933;">;</span></pre></div></div>

<p>There you go. There&#8217;s one catch, though. Google&#8217;s terms and conditions disallow using the JSON requests outside the context of a Google Map. So it can&#8217;t be used to automatically populate a database for example.</p>
<h3>2. Open Street Map (OSM)</h3>
<p>Open Street Map offers a legal alternative, albeit a limited and not as accurate one as the Google API. The JSON request URL for OSM is as follows:</p>
<pre class="qoate-code">http://nominatim.openstreetmap.org/reverse?format=json&amp;lat=37.34&amp;lon=-121.94</pre>
<p>This returns the following &#8211; compared to the Google output rather simple &#8211; output:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
     <span style="color: #3366CC;">&quot;place_id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;16539418&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;licence&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Data Copyright OpenStreetMap Contributors, Some Rights Reserved. CC-BY-SA 2.0.&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;osm_type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;way&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;osm_id&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;8936657&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;display_name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Highland Court, Santa Clara, Santa Clara County, California, 95050, United States of America&quot;</span><span style="color: #339933;">,</span>
     <span style="color: #3366CC;">&quot;address&quot;</span><span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;road&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Highland Court&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;city&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Santa Clara&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;county&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;Santa Clara County&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;state&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;California&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;postcode&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;95050&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;country&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;United States of America&quot;</span><span style="color: #339933;">,</span>
          <span style="color: #3366CC;">&quot;country_code&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;us&quot;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The corresponding PHP code is pretty much the same as already stated above but for clarity here is the whole block</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://nominatim.openstreetmap.org/reverse?format=json&amp;amp;lat=37.34&amp;amp;lon=-121.94'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>          <span style="color: #666666; font-style: italic;">//read the HTTP request</span>
<span style="color: #000088;">$jsondata</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>       <span style="color: #666666; font-style: italic;">//parse the JSOPN response</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$jsondata</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>                    <span style="color: #666666; font-style: italic;">//check data</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #000088;">$addr</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$jsondata</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'display_name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$addr</span><span style="color: #339933;">;</span></pre></div></div>

<p>As I said, the OSM data is not as accurate and for some regions doesn&#8217;t feature house numbers or suburb names. That is not to say that this won&#8217;t get better in time. </p>
<p>Compared to the Google request, OSM responses are quite slow, so parsing lots of addresses takes time. So PHP script execution time limits need to be considered.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2011/03/04/reverse-geocoding-via-the-open-street-map-and-google-maps-apis/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IE Ajax woes &#8211; Internet Explorer caching of Ajax requests</title>
		<link>http://www.tdmarketing.co.nz/blog/2010/11/09/ie-ajax-woes-internet-explorer-caching-of-ajax-requests/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2010/11/09/ie-ajax-woes-internet-explorer-caching-of-ajax-requests/#comments</comments>
		<pubDate>Mon, 08 Nov 2010 23:22:13 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Internet Explorer]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=13</guid>
		<description><![CDATA[It happened again. A stone old IE bug got me cold. For some strange reasons Internet Explorer caches Ajax requests. Which means that if database info is extracted via an Ajax GET request, Internet Explorer will store the Ajax response &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2010/11/09/ie-ajax-woes-internet-explorer-caching-of-ajax-requests/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>It happened again. A stone old IE bug got me cold. For some strange reasons Internet Explorer caches Ajax requests. Which means that if database info is extracted via an Ajax GET request, Internet Explorer will store the Ajax response in its cache for the current session. So if the database is updated during the session, Internet Explorer will show the zombie values from before the database update.</p>
<p>Best way to avoid this is to issue an Ajax POST request instead of GET.</p>
<p>More info <a href="http://ajaxian.com/archives/ajax-ie-caching-issue" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2010/11/09/ie-ajax-woes-internet-explorer-caching-of-ajax-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mac OS X Network &amp; NAS &#8211; the &#8220;Unix Executable&#8221; file type problem</title>
		<link>http://www.tdmarketing.co.nz/blog/2010/10/28/mac-os-x-network-nas-the-unix-executable-file-type-problem/</link>
		<comments>http://www.tdmarketing.co.nz/blog/2010/10/28/mac-os-x-network-nas-the-unix-executable-file-type-problem/#comments</comments>
		<pubDate>Wed, 27 Oct 2010 23:32:28 +0000</pubDate>
		<dc:creator>tomm</dc:creator>
				<category><![CDATA[Mac]]></category>
		<category><![CDATA[NAS]]></category>
		<category><![CDATA[Networking]]></category>

		<guid isPermaLink="false">http://www.tdmarketing.co.nz/blog/?p=5</guid>
		<description><![CDATA[So we are on an Apple Network here. But rather than relying on time machine as a backup solution &#8211; which isn&#8217;t really the way to go for multiple machine backups &#8211; we&#8217;re using Retrospect. (I could write a novel &#8230;<p class="read-more"><a href="http://www.tdmarketing.co.nz/blog/2010/10/28/mac-os-x-network-nas-the-unix-executable-file-type-problem/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>So we are on an Apple Network here. But rather than relying on time machine as a backup solution &#8211; which isn&#8217;t really the way to go for multiple machine backups &#8211; we&#8217;re using <a href="http://www.retrospect.com/" target="_blank">Retrospect</a>. (I could write a novel about the grief that piece of software has caused me &#8211; but why bother?). And while we ran our backups for yonks onto DVDs, the stack of media just took on ridiculous dimensions. That is why we installed a NAS (LevelOne FNS-5000B, good hardware, but shoddy interface, should have gone with a QNAP), with two 1.5 Tb hard disk in a <a href="http://en.wikipedia.org/wiki/RAID" target="_blank">RAID1</a> configuration. The NAS offers multiple protocols to connect to the network.</p>
<p>Retrospect is maintaining its backup files via the network on the NAS. And out of convenience (and for security reasons) we also keep older backup sets on the NAS. Now some of our guys have the Retrospect software (not just the client) installed one their machine so that they can access and retrieve files from historic backup sets.</p>
<p>And that&#8217;s where we encountered problems on a random basis. Sometimes on the Macs Retrospect simply refused to accept the backup files on the NAS as valid Retrospect catalogues. In the finder, the files did not show up with their usual icon, but instead featured the wee terminal icon. Apple-I showed the files as &#8220;Unix Executables&#8221;. Strangey enough, this problem would occur on some machine one day, but would be miraculously fixed the next day. If it did not fix itself I used to fix the issue by using a tool called <a href="http://www.frederikseiffert.de/filetype/" target="_blank">FileType</a> to change file type and creator codes. However, for some reason that did not work more recently. Plus I never felt quite comfortable fiddling with the backup set files anyhow.</p>
<p>So I did a bit of research and found that the &#8220;Unix Executable&#8221; problem has something to do with Macs not being able to &#8220;<a href="http://macosx.com/forums/design-media/302267-what-unix-executable-file-why-doing.html" target="_blank">read the file&#8217;s resource fork</a>&#8220;. So I asked myself, why the resource fork was readable one day, but not the next and I figured that it might have something to do with the network protocoll the Macs were using to access the NAS.</p>
<p>Sure enough, when the problem reoccured today, I did an Apple-I and found that the NAS was mounted using the Samba protocoll (smb://nas/). Now since the NAS also support AFP I wondered whether connecting via that protocol would fix the problem.</p>
<p>So I unmounted the NAS volume on the Mac (by clicking the eject icon next to the volume name in Finder) and then rather than using the Finder to re-connect to the NAS, I pressed Apple-K to get to the Finder&#8217;s  &#8220;Connect To Server&#8230;&#8221; window and typed in afp://192.168.1.* (i.e. our NAS IP). The NAS remounted into my finder and &#8211; voila! &#8211; gone was the &#8220;Unix Executable&#8221; problem.</p>
<p>Why the Macs sometimes connected via smb and then again via afp is beyond me. But at least now I know how to fix the problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.tdmarketing.co.nz/blog/2010/10/28/mac-os-x-network-nas-the-unix-executable-file-type-problem/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
