<?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>Cibul Tech Blog</title>
	<atom:link href="http://tech.cibul.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.cibul.net</link>
	<description>Fiddling with PHP, javascript and things</description>
	<lastBuildDate>Tue, 12 Mar 2013 16:10:42 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Phonegap &#8211; iOS push notifications</title>
		<link>http://tech.cibul.net/ios-push-notification-using-phonegap/</link>
		<comments>http://tech.cibul.net/ios-push-notification-using-phonegap/#comments</comments>
		<pubDate>Tue, 12 Mar 2013 15:51:27 +0000</pubDate>
		<dc:creator>omar</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=436</guid>
		<description><![CDATA[This tutorial is inspired from this one. A few changes were made to have things working with the 2.5 version of cordova.]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>This tutorial is inspired from <a  target="_blank" href="http://devgirl.org/2012/10/19/tutorial-apple-push-notifications-with-phonegap-part-1/">this one</a>. A few changes were made to have things working with the 2.5 version of cordova.</p>
<p><strong>Attention: The push notification can only work on a real device, not a simulator.</strong></p>
<h2>Steps:</h2>
<ul>
<li>What is the push notification?</li>
<li>How does it work?</li>
<li>Create a certificate.</li>
<li>Create a Cordova 2.5 project.</li>
<li>Integrate ios-phonegap-plugin to the project.</li>
<li>Add some native code.</li>
<li>Add some JavaScript to use the push notification API.</li>
<li>Server side</li>
</ul>
<h2>What is the push notification?</h2>
<p>If we ask wikipedia</p>
<p><strong><em>The Apple Push Notification Service is a service created by Apple Inc. that was launched together with iOS 3.0 on June 17, 2009… </em></strong>not really interesting</p>
<p>If we ask Apple</p>
<p><strong><em>It’s something who gonna revolutionize our phone &#8230; our life&#8230; the world!!</em></strong></p>
<p>So, what is realy the push notification?</p>
<p><strong><em>Push notification allows an app to notify you of new messages or events without the need to actually open the application, similar to how a text message will make a sound and pop up on your screen. This is a great way for apps to interact with us in the background, whether it be a game notifying us of some event occurring in our game world or simply the iPad&#8217;s mail application beeping as a new message appears in our inbox.</em></strong></p>
<div class="aligncenter"><a  target="_blank" href="http://ipad.about.com/od/iPad-Glossary/g/What-Is-Push-Notification.htm">source</a></div>
<h2>How does it work??</h2>
<div class="aligncenter"><img src="https://lh5.googleusercontent.com/pRmV6fgYlI_v_Ex7W-kXAIwPC62S8jNY7uR3UfSpabYh-CFdNAmWTpAQVhKOjvAdmupLaHlka0CvE7k77Sg3O1qNW0nVVbH4G0iWgghyZhrcRRgC4Bp-lY_Y2w" alt="" /></div>
<p>&nbsp;</p>
<ol>
<li>An app enables push notifications. The user has to confirm that he wishes to receive these notifications.</li>
<li>The app receives a “device token”. You can think of the device token as the address that push notifications will be sent to.</li>
<li>The app sends the device token to your server.</li>
<li>When something of interest to your app happens, the server sends a push notification to the Apple Push Notification Service, or APNS for short.</li>
<li>APNS sends the push notification to the user’s device.</li>
</ol>
<div class="aligncenter"><a  target="_blank" href="http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12">Source</a></div>
<h2>Create a certificate.</h2>
<p>Is it the first time you create a certificate for iOS? No? So you can move to the next section, just don’t forget to enable “Apple Push Notification service”.</p>
<div class="aligncenter"><img src="https://lh6.googleusercontent.com/d0zyf5w2RFBTBqc8cpa6cBJGe_3xAUEgj1BTM2WANSoZ0hqaVYAdA2yAcIgJr5O51rs_rPzkAjdBWCC2BV9ZD48bje9zBq9HBR5XTcf3CMOKLGJwAe8huS-nlA" alt="" /></div>
<p>In this case the certificate is used secure the connection between the server and the application. To make sure that no one else can send notifications to your applications.</p>
<p>You can use <a  target="_blank" href="http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12">this great tutorial</a> to create a certificate, without forgetting to, create the ck.pem file, we will use this later.</p>
<h2>Create a Cordova 2.5 project.</h2>
<p>If you don&#8217;t already have a Cordova project on your xcode, you can follow the <a  target="_blank" href="http://docs.phonegap.com/en/2.5.0/guide_getting-started_ios_index.md.html#Getting%20Started%20with%20iOS">getting started guide</a> of Cordova to create one.</p>
<h2>Integrate ios-phonegap-plugin to the project.</h2>
<div class="aligncenter"><img src="https://lh6.googleusercontent.com/1QaEStyI4LgzUMnoG4_YfhI0dVfysMKkAfH25kmR3PSTNbM_0E8AFJ24mTev7HJwVbFh65vwgFeY51NRynoWPcfcM4Eenmad1VbnkWM-MNycHQIDLO5-TxVhgA" alt="" /></div>
<p>After downloading the plugin from github, rename the bin folder to PushNotification, and drop it on the xcode project, then select Create groups for any added folders.</p>
<p>Drag and drop the plugin folder to the xcode project.</p>
<p>Now, from the finder, copy the PushNotification.js file to your www folder (or under a sub folder of www).</p>
<p>Then, add a script tag in your index.html to refer to it.</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;js/PushNotification.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></div>
<p>And finaly, and an entry to your config.xml file, underadd this</p>
<div class="codecolorer-container xml default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="xml codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp;</div></div>
<h2>Add some native code.</h2>
<p>First of all, because cordova2.5 does not include the JSONkit anymore, we have to make same changes.</p>
<p>Open the PushNotification.m</p>
<p>In the header, change the <code class="codecolorer cpp default"><span class="cpp"><span style="color: #339900;">#import</span></span></code> to <code class="codecolorer cpp default"><span class="cpp"><span style="color: #339900;">#import</span></span></code></p>
<p>In the didReceiveRemoteNotification method, change the</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NSString <span style="color: #000040;">*</span>jsStatement <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>NSString stringWithFormat<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;window.plugins.pushNotification.notificationCallback(%@);&quot;</span>, <span style="color: #008000;">&#91;</span>userInfo cdvjk_JSONString<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></div>
<p>To</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NSString <span style="color: #000040;">*</span>jsStatement <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>NSString stringWithFormat<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;window.plugins.pushNotification.notificationCallback(%@);&quot;</span>, <span style="color: #008000;">&#91;</span>userInfo JSONString<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span></div></div>
<p>Add the following code to your AppDelegate.m class before the @end to handle the notification events</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #339900;">#pragma PushNotification delegation</span><br />
<br />
<span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>application<span style="color: #008080;">:</span><span style="color: #008000;">&#40;</span>UIApplication<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>app didRegisterForRemoteNotificationsWithDeviceToken<span style="color: #008080;">:</span><span style="color: #008000;">&#40;</span>NSData<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>deviceToken<br />
<br />
<span style="color: #008000;">&#123;</span><br />
<br />
PushNotification<span style="color: #000040;">*</span> pushHandler <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>self.<span style="color: #007788;">viewController</span> getCommandInstance<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;PushNotification&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>pushHandler didRegisterForRemoteNotificationsWithDeviceToken<span style="color: #008080;">:</span>deviceToken<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>application<span style="color: #008080;">:</span><span style="color: #008000;">&#40;</span>UIApplication<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>app didFailToRegisterForRemoteNotificationsWithError<span style="color: #008080;">:</span><span style="color: #008000;">&#40;</span>NSError<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>error<br />
<br />
<span style="color: #008000;">&#123;</span><br />
<br />
PushNotification<span style="color: #000040;">*</span> pushHandler <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>self.<span style="color: #007788;">viewController</span> getCommandInstance<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;PushNotification&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>pushHandler didFailToRegisterForRemoteNotificationsWithError<span style="color: #008080;">:</span>error<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #000040;">-</span> <span style="color: #008000;">&#40;</span><span style="color: #0000ff;">void</span><span style="color: #008000;">&#41;</span>application<span style="color: #008080;">:</span><span style="color: #008000;">&#40;</span>UIApplication<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>application didReceiveRemoteNotification<span style="color: #008080;">:</span><span style="color: #008000;">&#40;</span>NSDictionary<span style="color: #000040;">*</span><span style="color: #008000;">&#41;</span>userInfo<br />
<br />
<span style="color: #008000;">&#123;</span><br />
<br />
PushNotification<span style="color: #000040;">*</span> pushHandler <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>self.<span style="color: #007788;">viewController</span> getCommandInstance<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;PushNotification&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
NSMutableDictionary<span style="color: #000040;">*</span> mutableUserInfo <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>userInfo mutableCopy<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #666666;">// Get application state for iOS4.x+ devices, otherwise assume active</span><br />
<br />
UIApplicationState appState <span style="color: #000080;">=</span> UIApplicationStateActive<span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#91;</span>application respondsToSelector<span style="color: #008080;">:</span>@selector<span style="color: #008000;">&#40;</span>applicationState<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
<br />
appState <span style="color: #000080;">=</span> application.<span style="color: #007788;">applicationState</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#91;</span>mutableUserInfo setValue<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;0&quot;</span> forKey<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;applicationLaunchNotification&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">if</span> <span style="color: #008000;">&#40;</span>appState <span style="color: #000080;">==</span> UIApplicationStateActive<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #008000;">&#91;</span>mutableUserInfo setValue<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;1&quot;</span> forKey<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;applicationStateActive&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>pushHandler didReceiveRemoteNotification<span style="color: #008080;">:</span>mutableUserInfo<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span> <span style="color: #0000ff;">else</span> <span style="color: #008000;">&#123;</span><br />
<br />
<span style="color: #008000;">&#91;</span>mutableUserInfo setValue<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;0&quot;</span> forKey<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;applicationStateActive&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>mutableUserInfo setValue<span style="color: #008080;">:</span><span style="color: #008000;">&#91;</span>NSNumber numberWithDouble<span style="color: #008080;">:</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#91;</span>NSDate date<span style="color: #008000;">&#93;</span> timeIntervalSince1970<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#93;</span> forKey<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;timestamp&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>pushHandler.<span style="color: #007788;">pendingNotifications</span> addObject<span style="color: #008080;">:</span>mutableUserInfo<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></div>
<p>And finally, into the didFinishLaunchingWithOptions method, add this code before the return YES to handle opening the application from a push notification</p>
<div class="codecolorer-container cpp default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="cpp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">NSDictionary<span style="color: #000040;">*</span> userInfo <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>launchOptions objectForKey<span style="color: #008080;">:</span>UIApplicationLaunchOptionsRemoteNotificationKey<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>userInfo<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span><br />
<br />
PushNotification <span style="color: #000040;">*</span>pushHandler <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>self.<span style="color: #007788;">viewController</span> getCommandInstance<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;PushNotification&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
NSMutableDictionary<span style="color: #000040;">*</span> mutableUserInfo <span style="color: #000080;">=</span> <span style="color: #008000;">&#91;</span>userInfo mutableCopy<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>mutableUserInfo setValue<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;1&quot;</span> forKey<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;applicationLaunchNotification&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>mutableUserInfo setValue<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;0&quot;</span> forKey<span style="color: #008080;">:</span>@<span style="color: #FF0000;">&quot;applicationStateActive&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#91;</span>pushHandler.<span style="color: #007788;">pendingNotifications</span> addObject<span style="color: #008080;">:</span>mutableUserInfo<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span><br />
<br />
<span style="color: #008000;">&#125;</span></div></div>
<h2>Add some JavaScript to use the push notification API.</h2>
<p>Add on onResume listener next to your ondeviceready listener, to handle the case when the application receives a push message while it is running in the background.</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'resume'</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">onResume</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>and put the onResume function next to onDevice ready</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">onResume<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><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> pushNotification <span style="color: #339933;">=</span> window.<span style="color: #660066;">plugins</span>.<span style="color: #660066;">pushNotification</span><span style="color: #339933;">;</span><br />
<br />
pushNotification.<span style="color: #660066;">getPendingNotifications</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>notifications<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
console.<span style="color: #660066;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'getPendingNotifications:%o'</span><span style="color: #339933;">,</span> notifications<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> notifs <span style="color: #339933;">=</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>notifications.<span style="color: #660066;">notifications</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>notifs<span style="color: #339933;">!=</span><span style="color: #3366CC;">'[]'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
navigator.<span style="color: #660066;">notification</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>notifications.<span style="color: #660066;">notifications</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">aps</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;log&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;log&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;<br />
&quot;</span> <span style="color: #339933;">+</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>notifications.<span style="color: #660066;">notifications</span><span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">aps</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #009900;">&#125;</span></div></div>
<p>In the on device ready, add the following code to handle, the case of the application recive a push when she is active, register the application to the APNs server, and the case where the application is closed while received a push</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">document.<span style="color: #660066;">addEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'push-notification'</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//alert(event);</span><br />
<br />
console.<span style="color: #660066;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'push-notification!:%o'</span><span style="color: #339933;">,</span> event.<span style="color: #660066;">notification</span>.<span style="color: #660066;">aps</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;log&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;log&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;<br />
&quot;</span> <span style="color: #339933;">+</span> JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">notification</span>.<span style="color: #660066;">aps</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">;</span><br />
<br />
navigator.<span style="color: #660066;">notification</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span>JSON.<span style="color: #660066;">stringify</span><span style="color: #009900;">&#40;</span>event.<span style="color: #660066;">notification</span>.<span style="color: #660066;">aps</span>.<span style="color: #000066;">alert</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">//console.log('push-notification!:%o'+ event.push-notification);</span><br />
<br />
<span style="color: #006600; font-style: italic;">//navigator.notification.alert(JSON.stringify(['push-notification!', event]));</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #003366; font-weight: bold;">var</span> pushNotification <span style="color: #339933;">=</span> window.<span style="color: #660066;">plugins</span>.<span style="color: #660066;">pushNotification</span><span style="color: #339933;">;</span><br />
<br />
pushNotification.<span style="color: #660066;">registerDevice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #000066;">alert</span><span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> badge<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> sound<span style="color: #339933;">:</span><span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// if successful status is an object that looks like this:</span><br />
<br />
<span style="color: #006600; font-style: italic;">// {&quot;type&quot;:&quot;7&quot;,&quot;pushBadge&quot;:&quot;1&quot;,&quot;pushSound&quot;:&quot;1&quot;,&quot;enabled&quot;:&quot;1&quot;,&quot;deviceToken&quot;:&quot;blablahblah&quot;,&quot;pushAlert&quot;:&quot;1&quot;}</span><br />
<br />
console.<span style="color: #660066;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'registerDevice:%o'</span><span style="color: #339933;">,</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span><span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">//navigator.notification.alert(JSON.stringify(['registerDevice', status]));</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
pushNotification.<span style="color: #660066;">getPendingNotifications</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>notifications<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
console.<span style="color: #660066;">warn</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'getPendingNotifications:%o'</span><span style="color: #339933;">,</span> notifications<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #006600; font-style: italic;">// navigator.notification.alert(JSON.stringify(['getPendingNotifications', notifications]));</span><br />
<br />
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
app.<span style="color: #660066;">receivedEvent</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'deviceready'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h2>Server side</h2>
<p>You can use the simple php script from <a  target="_blank" href="http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12">this link</a>, it’s just to to send requests to the APNS server.</p>
<h2>Test on a real device</h2>
<div class="aligncenter"><img src="https://lh5.googleusercontent.com/cubiiEMD9iq3tdfn-LmUufDOM8Ttcjy6EID7SWCNXYPZ9it05apek_fxa53uY79s1mTJeutqf0-mvYTrgpVkCeKuYsJ42aaVEHNrjA0l4-87A-TRuJgE8nG5ow" alt="" /></div>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=436&amp;md5=ba9f88a3877d5dd208861890dc430619" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/ios-push-notification-using-phonegap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>CibulCalendar &#8211; A javascript date range picker</title>
		<link>http://tech.cibul.net/cibulcalendar-a-date-range-picker/</link>
		<comments>http://tech.cibul.net/cibulcalendar-a-date-range-picker/#comments</comments>
		<pubDate>Sun, 10 Mar 2013 16:01:07 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=357</guid>
		<description><![CDATA[A javascript date picker that enables users to easily pick dates or date ranges.]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><h2>Overview</h2>
<p>CibulCalendar is a date picker that enables users to easily pick dates or date ranges.</p>
<p>You can download the latest version by clicking <a href="https://github.com/kaore/CibulCalendar/archive/master.zip">here</a> or visit the github <a href="https://github.com/kaore/CibulCalendar">repo</a>. The minified version of the script is around 10.9kb.</p>
<p>Here is a <a href="http://bit.ly/Wixita" target="_blank">demo</a> that shows how to select a range with a mouse drag&#038;drop or a touch and drag gesture:</p>
<p><br/></p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #000066;">onload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; setCibulCalendar<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'basic'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will stick to element &lt;input type=&quot;text&quot; id=&quot;basic&quot;/&gt;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<div class="aligncenter basicexample">
<input type="text" id="basic"/>
</div>
<style>
.basicexample { margin: 40px 0; }
.ccal { width: 18em; font-size: 0.8em; margin-bottom: 5px;}
.ccal ul { margin: 0; padding: 0!important; text-align: center; }
.ccal li { list-style-type: none; display: inline-block; width: 13.2%; cursor: pointer; text-align: center; margin:0; }
.ccal li span { display: inline-block; line-height: 1.8em; }
.ccal li.calmonth { width: 66%; }
.ccal li span { padding: 0.1em 0.05em; display: block; }
.ccal li.calprev span, .ccal li.calnext span { color: #aaa; }
.ccal .calbody li.selected span { background: #666; color: white; }
.ccal .calbody li.preselected span { background: #f0f0f0; }
.ccal * { -moz-user-select: -moz-none; -khtml-user-select: none; -webkit-user-select: none; -ms-user-select: none; user-select: none; }
.today { font-weight: bold; }
.calendar-canvas {
  text-align: center;
  background: white;
  v-moz-box-shadow: 0 3px 4px #999999; 
  -moz-box-shadow: 0 3px 4px #999999; 
  -webkit-box-shadow: 0 3px 4px #999999; 
  box-shadow: 0 1px 2px #999999;
}
</style>
<p>It is also used in the header of the website <a href="http://cibul.net">cibul.net</a>. It works on most major modern browser and IE 8.0+ as well as Android and iOS devices.</p>
<p>Features include:</p>
<ul>
<li>Drag and drop range selection</li>
<li>Single date selection</li>
<li>Multilingual</li>
<li>Helper function for single line setup</li>
<li>Fully Customizable (see details below)</li>
</ul>
<h2>How to use</h2>
<p>There are two ways to use the calendar: </p>
<ul>
<li><code class="codecolorer javascript default"><span class="javascript">setCibulCalendar<span style="color: #009900;">&#40;</span>id<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span></span></code>: simplest setup for use with an input field.</li>
<li><code class="codecolorer javascript default"><span class="javascript"><span style="color: #003366; font-weight: bold;">var</span> cCal <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> CibulCalendar<span style="color: #009900;">&#40;</span>canvasElem<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span></span></code>: more flexible approach to handle date selections as you wish through a callback and keep a reference to the calendar for later use.</li>
</ul>
<h2>Options</h2>
<ul>
<li><strong>range (default: true)</strong>: if false, range date picking is disabled. Here is an <a href="http://bit.ly/YeYdWD">example</a>.</li>
<li><strong>enabled (default: true)</strong>: state of the calendar at init</li>
<li><strong>lang (default: &#8216;en&#8217;)</strong>: Language of the calendar. can be english (en), french (fr), italian (it) or spanish (es). You can extend this with any other language (see below).</li>
<li><strong>firstDayOfWeek (default: 1)</strong>: first day of the week on the calendar. 0 for sunday and onwards.</li>
<li><strong>selected (default: false)</strong>: default selection at initialization. Should be a object: {begin: Date, end: Date}</li>
<li><strong>filter</strong>: (default: false): callback called on each calendar render to handle date classes. If set, function is given a date and an array of classes and should return the array of classes that will be set on the rendered calendar. Click <a href="http://bit.ly/XE6K6A">here</a> for an example highlighting weekends.</li>
<li><strong>navDomContent</strong> (default: {prev: &#8216;<', next: '>&#8216;}): html content for the month navigation links. In case you want to set something more fancy like icons or images.</li>
<li><strong>classes</strong>: in case you want to change the name of the classes used in the calendar.</li>
<li><strong>monthNames</strong>: add a new month set in the language of your choice. See <a href="http://bit.ly/WixNUb">here</a> for an example of a calendar in icelandic</li>
<li><strong>weekDays</strong>: add a new week set in the language of your choice</li>
<li><strong>switchMonthOnHoverDelay</strong>: on a range selection spanning over multiple months, the delay during which next month days should be hovered over before the calendar goes to the neighboring month</li>
</ul>
<h2>Methods</h2>
<ul>
<li><strong>disable</strong>: disable the calendar</li>
<li><strong>enable</strong>: enable the calendar</li>
<li><strong>showNext</strong>: render following month</li>
<li><strong>showPrevious</strong>: render previous month</li>
<li><strong>setSelected(newSelection, updateMonth)</strong>: set new selection. newSelection is an object containing two indexes: begin and end, each referring to a date. updateMonth is a boolean indicating if the month of the start of the selection should be displayed.</li>
</ul>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=357&amp;md5=95a28dbc5407f981f33b3af5a4d9648c" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/cibulcalendar-a-date-range-picker/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upload and display an image without a page refresh</title>
		<link>http://tech.cibul.net/upload-and-display-an-image-without-a-page-refresh/</link>
		<comments>http://tech.cibul.net/upload-and-display-an-image-without-a-page-refresh/#comments</comments>
		<pubDate>Sat, 23 Feb 2013 12:07:40 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=381</guid>
		<description><![CDATA[Here we will see how to upload an image without a page refresh using an iframe and some javascript. There are a bunch of jquery plugins that do this, but you'll see here that there is nothing really complicated about it and it can easily be achieved with a couple of javascript lines.]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>Here we will see how to upload an image without a page refresh using an iframe and some javascript.</p>
<p>There are a bunch of jquery plugins that do this, but you&#8217;ll see here that there is nothing really complicated about it and it can easily be achieved with a couple of javascript lines.</p>
<h2>Principle</h2>
<p>Normally when you want to upload an image to the server you create a post form with a &#8216;file&#8217; input to allow the user to pick the image, and a submit button for posting the contents to the server. The user clicks on &#8216;Choose file&#8217;, picks his image and clicks on submit&#8230; and sees the browser waiting for a full page response after the file is uploaded.</p>
<p>The trick to avoid this full page refresh is to use a &#8216;target&#8217; attribute on the form and have it point to an iframe.  As the submit button is clicked, the browser will load up the response in the targeted iframe without refreshing the whole page.</p>
<p>Try it with the following code. First, the form:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload.php&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span> <span style="color: #000066;">target</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;leiframe&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/iframe.html"><span style="color: #000000; font-weight: bold;">iframe</span></a> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;leiframe&quot;</span> <span style="color: #000066;">width</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">height</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;200&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/iframe.html"><span style="color: #000000; font-weight: bold;">iframe</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></div>
<p><span class="caption">Pretty simple.. Note the target attribute with the name of the iframe</span></p>
<p>Second, the upload script:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;?php</span><br />
<br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">//</span> I<span style="color: #ff0000;">'m skipping a lloooot of verification steps on the file here.</span><br />
<span style="color: #009900;"> &nbsp;// Also, I'</span>m assuming you uploaded a jpg.</span><br />
<br />
<span style="color: #009900;"> &nbsp;move_uploaded_file<span style="color: #66cc66;">&#40;</span>$_FILES<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'image'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'tmp_name'</span><span style="color: #66cc66;">&#93;</span>, <span style="color: #ff0000;">'uploaded/pic.jpg'</span><span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;">?&gt;</span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">style</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;width: 100%;&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;uploaded/pic.jpg?v=&lt;?php echo rand(0,1000) // rand() prevents the browser from displaying a previously cached image ?&gt;</span></span>&quot;/&gt;<br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></div>
<p><span class="caption">Nothing more.</span></p>
<p>Already with this, you have a basic solution which uploads and displays images without a page refresh, and without having event started writing any javascript.</p>
<div class="aligncenter"><img src="http://tech.cibul.net/wp-content/uploads/2013/02/1.jpeg" alt="the form"/></div>
<p><span class="caption">Pure eye candy.</span></p>
<p>But with javascript it is easy to come up with a more flexible solution in only a few steps:</p>
<ol>
<li>First, we&#8217;ll have a page both serving up the form and processing it. Normally two different resources handle this to avoid reposts, but we&#8217;ll keep it simple for now.</li>
<li>Then, we prepare a javascript function that handles the iframe creation and form tweaking to make a sleeker image submission</li>
<li>That function will change the destination of the form post, to a script which we write to process the image and prepare the content for the iframe.</li>
<li>And finally, we write a function to handle the response</li>
</ol>
<h2>A regular form submission</h2>
<p>The following form submits the image and serves up the result directly with a full refresh:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;?php</span><br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">//</span> Process image</span><br />
<span style="color: #009900;"> &nbsp;$uploadName <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'uploaded/pic.jpg'</span>;</span><br />
<span style="color: #009900;"> &nbsp;if <span style="color: #66cc66;">&#40;</span>count<span style="color: #66cc66;">&#40;</span>$_FILES<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> move_uploaded_file<span style="color: #66cc66;">&#40;</span>$_FILES<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'image'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'tmp_name'</span><span style="color: #66cc66;">&#93;</span>, $uploadName<span style="color: #66cc66;">&#41;</span>;</span><br />
<span style="color: #009900;">?&gt;</span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; li { display: inline-block; padding: 0.2em; width: 2em;}<br />
&nbsp; &nbsp; &nbsp; img { width: 100%; }<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a> <span style="color: #000066;">onload</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;onload()&quot;</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;index.php&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;image&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/input.html"><span style="color: #000000; font-weight: bold;">input</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;upload&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/form.html"><span style="color: #000000; font-weight: bold;">form</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;?php if <span style="color: #66cc66;">&#40;</span>count<span style="color: #66cc66;">&#40;</span>$_FILES<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>: ?&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&lt;?php echo $uploadName ?&gt;</span></span>&quot;/&gt;<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/li.html"><span style="color: #000000; font-weight: bold;">li</span></a>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;?php endif; ?&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/ul.html"><span style="color: #000000; font-weight: bold;">ul</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; function onload(){}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></div>
<p><span class="caption">index.php</span></p>
<p>Your form works now fine on its own, and reloads itself with whatever image was submitted.</p>
<h2>The js function enabling the iframe</h2>
<p>Now we need a function that replicates the behavior described earlier in this tutorial: it needs to create an iframe, and have the form point to it through a target attribute. The function will also divert the submission to a dedicated php script that will generate the contents of the iframe. Here it is (don&#8217;t forget to add a reference to it in your main file (<code class="codecolorer html4strict default"><span class="html4strict"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;iframeSubmit.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></span></code>:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #003366; font-weight: bold;">function</span> iframeSubmit<span style="color: #009900;">&#40;</span>formElem<span style="color: #339933;">,</span> action<span style="color: #339933;">,</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// we name a callback that will be called from inside the iframe</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> callbackName <span style="color: #339933;">=</span> <span style="color: #3366CC;">'iframe'</span> <span style="color: #339933;">+</span> Math.<span style="color: #660066;">ceil</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">random</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #CC0000;">10000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; action <span style="color: #339933;">=</span> action <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span>action.<span style="color: #660066;">indexOf</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: #CC0000;">1</span><span style="color: #339933;">?</span><span style="color: #3366CC;">'?'</span><span style="color: #339933;">:</span><span style="color: #3366CC;">'&amp;'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'callback='</span> <span style="color: #339933;">+</span> callbackName<span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// we create an iframe and use the callback as its name (why not).</span><br />
&nbsp; <span style="color: #003366; font-weight: bold;">var</span> iframe <span style="color: #339933;">=</span> document.<span style="color: #660066;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'iframe'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; iframe.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">,</span> callbackName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; iframe.<span style="color: #660066;">style</span>.<span style="color: #660066;">display</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// we add the target and edit the action of the form</span><br />
&nbsp; formElem.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'target'</span><span style="color: #339933;">,</span> callbackName<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; formElem.<span style="color: #660066;">setAttribute</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'action'</span><span style="color: #339933;">,</span> action<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #006600; font-style: italic;">// we add the hidden iframe after the form</span><br />
&nbsp; formElem.<span style="color: #660066;">parentNode</span>.<span style="color: #660066;">appendChild</span><span style="color: #009900;">&#40;</span>iframe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; window<span style="color: #009900;">&#91;</span>callbackName<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> callback<span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<h2>Preparing the response for the iframe</h2>
<p>When we use the iframeSubmit function, we&#8217;ll have the form point to upload.php for image submissions and the result will be loaded within the iframe (which is now a hidden element in the page). The upload.php script should process the submission and generate an iframe content which will call the function defined and passed by iframeSubmit, with the results of the operation. The function will be called from within the iframe, which is possible only when the page and iframes have the same domain origin. Here it is:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;?php</span><br />
<br />
<span style="color: #009900;"> &nbsp;<span style="color: #66cc66;">//</span> I<span style="color: #ff0000;">'m skipping a lloooot of verification steps on the file here.</span><br />
<span style="color: #009900;"> &nbsp;// Also, I'</span>m assuming you uploaded a jpg.</span><br />
<span style="color: #009900;"> &nbsp;$imagePath <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'uploaded/pic.jpg'</span>;</span><br />
<span style="color: #009900;"> &nbsp;move_uploaded_file<span style="color: #66cc66;">&#40;</span>$_FILES<span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'image'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'tmp_name'</span><span style="color: #66cc66;">&#93;</span>, $imagePath<span style="color: #66cc66;">&#41;</span>;</span><br />
<br />
<span style="color: #009900;">?&gt;</span><br />
<span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span>Iframe content<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&lt;?php echo <span style="color: #ff0000;">'window.parent[\'</span><span style="color: #ff0000;">' . $_GET['</span>callback<span style="color: #ff0000;">'] . '</span>\<span style="color: #ff0000;">']('</span> . json_encode<span style="color: #66cc66;">&#40;</span>array<span style="color: #66cc66;">&#40;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #ff0000;">'success'</span> <span style="color: #66cc66;">=</span>&gt;</span> true, <br />
&nbsp; &nbsp; &nbsp; &nbsp; 'path' =&gt; $imagePath<br />
&nbsp; &nbsp; &nbsp; )) . ')' ?&gt;<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></div>
<p>There. Once the response of upload.php is loaded in the iframe, it will only contain the execution of the callback defined in the iframeSubmit script</p>
<h2>Wrapping it up</h2>
<p>The only thing missing now is the bit in the index page where the javascript function is called and the response processed. Add this to the end of the index page: </p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
<br />
&nbsp; &nbsp; &nbsp; function processResponse(response) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (!response.success) {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert('woopsie');<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; var newListItem = document.createElement('li');<br />
&nbsp; &nbsp; &nbsp; &nbsp; newListItem.innerHTML = '<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/img.html"><span style="color: #000000; font-weight: bold;">img</span></a> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;' + response.path + '&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>';<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; document.getElementsByTagName('ul')[0].appendChild(newListItem);<br />
&nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &nbsp; function onload() {<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; iframeSubmit(document.getElementsByTagName('form')[0], 'upload.php', processResponse);<br />
<br />
&nbsp; &nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span></div></div>
<p>There, you can put in the callback of the iframeSubmit function all the image handling you want! (The function here only piles up the images received in a list)</p>
<div class="aligncenter"><img src="http://tech.cibul.net/wp-content/uploads/2013/02/2.png" alt="the form"/></div>
<p><span class="caption">Just wow.</span></p>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=381&amp;md5=4d200801a7c4ffc57c0b85cf4b5637bf" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/upload-and-display-an-image-without-a-page-refresh/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Javascript function for a slide effect on pan/slide gestures or mouse drags</title>
		<link>http://tech.cibul.net/javascript-function-for-a-slide-effect-on-panslide-gestures-or-mouse-drags/</link>
		<comments>http://tech.cibul.net/javascript-function-for-a-slide-effect-on-panslide-gestures-or-mouse-drags/#comments</comments>
		<pubDate>Wed, 19 Dec 2012 18:16:43 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=325</guid>
		<description><![CDATA[Here is a function that transitions a pane element to a new one by using either a drag of your mouse or a pan/slide gesture on touch enabled devices.

Give it a try if you are looking for a simple solution to handle pane transition or a tab navigation system without using a fully featured ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>Here is a function that transitions a pane element to a new one by using either a drag of your mouse or a pan/slide gesture on touch enabled devices.</p>
<p>Give it a try if you are looking for a simple solution to handle pane transition or a tab navigation system without using a fully featured tab navigation script.</p>
<div id="slide-canvas">
<div id="pane1">
    <img src="http://tech.cibul.net/wp-content/uploads/2012/12/awwyiss1.jpg"/>
  </div>
</div>
<div class="aligncenter"><i>Drag the pane to the left, or wait for the transition to be triggered</i></div>
<style>
  #slide-canvas { width:300px; height: 300px; text-align: center; margin: 20px auto; }
</style>
<p><br/></p>
<h2>Basic usage</h2>
<p>Pass it the element you want to transition from, specifying the elements you want to transition to, as such:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; slide(paneElement, {right: newPaneElement});</div></div>
<p>The function returns a trigger function, should you want to call the transition by yourself.</p>
<p>You can define several slide directions with different destination panes by just passing them in the second parameter like this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; slide(paneElement, {right: newRightPaneElement, left: newLeftPaneElement, top: newTopPaneElement, bottom: newBottomPaneElement });</div></div>
<p>Once the transition is complete, all but the displayed element are removed from the dom.</p>
<p>You&#8217;ll find a simple demo page <a href="http://bit.ly/T7sQe1" target="_blank">here</a> and a github library <a href="https://github.com/kaore/slide">here</a></p>
<p><br/></p>
<h2>Compatibility</h2>
<p>This was tested with IE8, chrome, firefox, opera and mobile webkit browsers. Write me a note if you have a browser (a recent-ish one) that throws errors</p>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=325&amp;md5=283bfd08c60daf1956b9e780b2c2e9cf" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/javascript-function-for-a-slide-effect-on-panslide-gestures-or-mouse-drags/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Phonegap &#8211; Using the iOS LocalNotification plugin</title>
		<link>http://tech.cibul.net/localnotification-ios/</link>
		<comments>http://tech.cibul.net/localnotification-ios/#comments</comments>
		<pubDate>Wed, 21 Nov 2012 15:47:07 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=315</guid>
		<description><![CDATA[The last post was about deploying the plugin offering the same functionality on and android device, here is how to get the iOS version running. Find the github page of the plugin here. There's also a more detailed procedure here

  Get a basic phonegap project running
  Get the plugin files. Copy the ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>The last post was about deploying the plugin offering the same functionality on and android device, here is how to get the iOS version running. Find the github page of the plugin <a href="https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/LocalNotification" title="LocalNotification iPhone plugin">here</a>. There&#8217;s also a more detailed procedure <a href="http://www.drewdahlman.com/meusLabs/?p=117">here</a></p>
<ol>
<li>Get a basic phonegap project running</li>
<li>Get the plugin files. Copy the LocalNotification.h and .m in your project&#8217;s Plugins xcode folder and the plugin js file in the www folder.</li>
<li>If you are using cordova 1.7 or above, the content of LocalNotification.h should be something like:
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">//<br />
// &nbsp;LocalNotification.h<br />
//&nbsp; Phonegap LocalNotification Plugin<br />
//&nbsp; Copyright (c) Greg Allen 2011 &amp; 2012 Drew Dahlman<br />
//&nbsp; MIT Licensed<br />
<br />
#import &lt;Foundation/Foundation.h&gt;<br />
<br />
#ifdef CORDOVA_FRAMEWORK<br />
#import &lt;Cordova/CDVPlugin.h&gt;<br />
#else<br />
#import &quot;CDVPlugin.h&quot;<br />
#endif<br />
<br />
@interface LocalNotification : CDVPlugin {<br />
&nbsp; &nbsp; <br />
}<br />
- (void)addNotification:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;<br />
- (void)cancelNotification:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;<br />
- (void)cancelAllNotifications:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;<br />
<br />
@end</div></div>
</li>
<li>
    Open AppDelegate.m in the Classes folder and add this:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">// ADD OUR NOTIFICATION CODE<br />
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification <br />
{<br />
<br />
&nbsp; &nbsp; UIApplicationState state = [application applicationState];<br />
&nbsp; &nbsp; if (state == UIApplicationStateActive) {<br />
&nbsp; &nbsp; // WAS RUNNING<br />
&nbsp; &nbsp; &nbsp; NSLog(@&quot;I was currently active&quot;);<br />
<br />
&nbsp; &nbsp; &nbsp; NSString *notCB = [notification.userInfo objectForKey:@&quot;foreground&quot;];<br />
&nbsp; &nbsp; &nbsp; NSString *notID = [notification.userInfo objectForKey:@&quot;notificationId&quot;];<br />
<br />
&nbsp; &nbsp; &nbsp; NSString * jsCallBack = [NSString <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stringWithFormat:@&quot;%@(%@)&quot;, notCB,notID]; &nbsp;<br />
<br />
<br />
&nbsp; &nbsp; &nbsp; [self.viewController.webView &nbsp;stringByEvaluatingJavaScriptFromString:jsCallBack];<br />
<br />
&nbsp; &nbsp; &nbsp; application.applicationIconBadgeNumber = 0;<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; else {<br />
&nbsp; &nbsp; &nbsp; &nbsp; // WAS IN BG<br />
&nbsp; &nbsp; &nbsp; &nbsp; NSLog(@&quot;I was in the background&quot;);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; NSString *notCB = [notification.userInfo objectForKey:@&quot;background&quot;];<br />
&nbsp; &nbsp; &nbsp; &nbsp; NSString *notID = [notification.userInfo objectForKey:@&quot;notificationId&quot;];<br />
<br />
&nbsp; &nbsp; &nbsp; NSString * jsCallBack = [NSString <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;stringWithFormat:@&quot;%@(%@)&quot;, notCB,notID]; <br />
&nbsp; &nbsp; &nbsp; &nbsp; [self.viewController.webView stringByEvaluatingJavaScriptFromString:jsCallBack]; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; application.applicationIconBadgeNumber = 0;<br />
&nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
}</div></div>
</li>
<li>Open the Cordova.plist file, and add an entry in the Plugins dictionary. Key: LocalNotification, type: String and value: LocalNotification.</li>
<li>
    Setup the test project. index.html:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&nbsp; &lt;head&gt;<br />
&nbsp; &lt;title&gt;&lt;/title&gt;<br />
&nbsp; <br />
&nbsp; &nbsp; &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;&quot; /&gt;<br />
&nbsp; &nbsp; &lt;meta charset=&quot;utf-8&quot;&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot; src=&quot;cordova-1.7.0.js&quot;&gt;&lt;/script&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot; src=&quot;LocalNotification.js&quot;&gt;&lt;/script&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot; src=&quot;init.js&quot;&gt;&lt;/script&gt;<br />
&nbsp; &nbsp; &lt;link rel=&quot;stylesheet&quot; href=&quot;style.css&quot; /&gt;<br />
&nbsp; &nbsp; <br />
&nbsp; &lt;/head&gt;<br />
&nbsp; &lt;body onload=&quot;app.bodyLoad()&quot;&gt;<br />
&nbsp; &lt;div id=&quot;header&quot;&gt;&lt;/div&gt;<br />
&nbsp; &nbsp; &lt;div class=&quot;btn&quot; onClick=&quot;notification.local_min();&quot;&gt;localNotification + 60 seconds&lt;/div&gt;<br />
&nbsp; &nbsp; &lt;div class=&quot;btn&quot; onClick=&quot;notification.local_timed(13,30);&quot;&gt;localNotification&lt;/div&gt;<br />
&nbsp; &nbsp; &lt;div class=&quot;btn&quot; onClick=&quot;notification.tomorrow(06,05,1);&quot;&gt;localNotification tomorrow&lt;/div&gt;<br />
&nbsp; &nbsp; &lt;div class=&quot;btn&quot; onClick=&quot;notification.clear();&quot;&gt;clear all&lt;/div&gt;<br />
&nbsp; &lt;/body&gt;<br />
&lt;/html&gt;</div></div>
<p>init.js:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">// JavaScript Document<br />
// PROJECT: Phonegap LocalNotifications<br />
// AUTHOR: Drew Dahlman ( www.drewdahlman.com )<br />
// DATE: 1.26.2012<br />
<br />
/*<br />
NOTES:<br />
We will be creating LocalNotifications that can be set to fire while app is inactive, <br />
and create a callback for the JS to know when the app has come back from a notification.<br />
<br />
One thing that is deceptive about the LocalNotifications plugin is that when it shows a notification<br />
has been created it shows it based on the timezone +0000 which can throw you off.<br />
<br />
in the call for setting the notification we simply call notification.local_timed(&quot;13:00&quot;) - note that I supplied a string.<br />
<br />
The ability to set repeating notifications has been added! <br />
- daily<br />
- weekly<br />
- monthly<br />
- yearly<br />
<br />
<br />
*/<br />
<br />
<br />
// NOTIFICATION CENTER<br />
/*<br />
I like to set up one object that's only job is to manage notifications<br />
*/<br />
var notification = {<br />
&nbsp; &nbsp; init:function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; // This will fire after 60 seconds<br />
&nbsp; &nbsp; local_min:function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; var d = new Date();<br />
&nbsp; &nbsp; &nbsp; &nbsp; d = d.getTime() + 60*1000; //60 seconds from now<br />
&nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; plugins.localNotification.add({<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date: d,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat:'daily',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: 'This just fired after a minute!',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasAction: true,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; badge: 1,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: '1',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sound:'horn.caf',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background:'app.background',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreground:'app.running'<br />
&nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; // This will fire based on the time provided.<br />
&nbsp; &nbsp; // Something to note is that the iPhone goes off of 24hr time<br />
&nbsp; &nbsp; // it defaults to no timezone adjustment so +0000 !IMPORTANT<br />
&nbsp; &nbsp; local_timed:function(hh,mm){<br />
&nbsp; &nbsp; &nbsp; &nbsp; // the example time we provide is 13:00<br />
&nbsp; &nbsp; &nbsp; &nbsp; // This means the alarm will go off at 1pm +0000<br />
&nbsp; &nbsp; &nbsp; &nbsp; // how does this translate to your time? While the phone schedules 1pm +0000<br />
&nbsp; &nbsp; &nbsp; &nbsp; // it will still go off at your desired time base on your phones time.<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; console.log(hh+&quot; &quot;+mm);<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Now lets make a new date<br />
&nbsp; &nbsp; &nbsp; &nbsp; var d = new Date();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = d.setSeconds(00);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = d.setMinutes(mm);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = d.setHours(hh);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; plugins.localNotification.add({<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date: d,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat:'daily',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: 'This went off just as expected!',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasAction: true,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; badge: 1,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: '2',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sound:'horn.caf',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background:'app.background',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreground:'app.running'<br />
&nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; clear:function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; plugins.localNotification.cancelAll();<br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; tomorrow:function(hh,mm,days){<br />
&nbsp; &nbsp; &nbsp; &nbsp; // Now lets make a new date<br />
&nbsp; &nbsp; &nbsp; &nbsp; var d = new Date();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = d.setSeconds(00);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = d.setMinutes(mm);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = d.setHours(hh);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // add a day<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = d.setDate(d.getDate()+days);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; d = new Date(d);<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; plugins.localNotification.add({<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date: d,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat:'daily',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message: 'This went off just as expected!',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hasAction: true,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; badge: 1,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: '3',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sound:'horn.caf',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background:'app.background',<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreground:'app.running'<br />
&nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; }<br />
&nbsp; &nbsp; <br />
}<br />
<br />
// APP<br />
var app = {<br />
&nbsp; &nbsp; bodyLoad:function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; document.addEventListener(&quot;deviceready&quot;, app.deviceReady, false);<br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; deviceReady:function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; app.init();<br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; init:function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; background:function(id){<br />
&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;I was in the background but i'm back now! ID=&quot;+id);<br />
&nbsp; &nbsp; },<br />
&nbsp; &nbsp; running:function(id){<br />
&nbsp; &nbsp; &nbsp; &nbsp; console.log(&quot;I am currently running, what should I do? ID=&quot;+id);<br />
&nbsp; &nbsp; }<br />
};</div></div>
<p>style.css:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">* {<br />
&nbsp; &nbsp; margin:0px;<br />
}<br />
body {<br />
&nbsp; &nbsp; font-family:Helvetica;<br />
&nbsp; &nbsp; -webkit-user-select: none;<br />
&nbsp; &nbsp; background-color:#fff;<br />
}<br />
#wrapper {<br />
&nbsp; &nbsp; width:100%;<br />
&nbsp; &nbsp; height:100%;<br />
&nbsp; &nbsp; padding-top:15px;<br />
}<br />
#header {<br />
&nbsp; &nbsp; height:150px;<br />
&nbsp; &nbsp; width:100%;<br />
&nbsp; &nbsp; background-image:url(../images/header.png);<br />
&nbsp; &nbsp; background-size:100%;<br />
&nbsp; &nbsp; margin-bottom:10px;<br />
&nbsp; &nbsp; box-shadow: 0px 0px 2px .6px #222222;<br />
}<br />
.btn {<br />
&nbsp; &nbsp; font-size:18px;<br />
&nbsp; &nbsp; text-align:center;<br />
&nbsp; &nbsp; padding-top:12px;<br />
&nbsp; &nbsp; padding-bottom:12px;<br />
&nbsp; &nbsp; width:95%;&nbsp; <br />
&nbsp; &nbsp; border-radius: 5px; <br />
&nbsp; &nbsp; background-color:#545454;<br />
&nbsp; &nbsp; margin:auto;<br />
&nbsp; &nbsp; color:#fff;<br />
&nbsp; &nbsp; font-weight:bold;<br />
&nbsp; &nbsp; box-shadow: 0px 0px 2px .6px #222222;<br />
&nbsp; &nbsp; margin-top:5px;<br />
}</div></div>
</li>
<li>Done!</li>
</ol>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=315&amp;md5=a02cda1a4221c9ddd8ef516a096391ef" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/localnotification-ios/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Phonegap &#8211; Using the Android LocalNotification plugin</title>
		<link>http://tech.cibul.net/using-the-android-localnotification-plugin-for-phonegap/</link>
		<comments>http://tech.cibul.net/using-the-android-localnotification-plugin-for-phonegap/#comments</comments>
		<pubDate>Mon, 05 Nov 2012 13:26:30 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=298</guid>
		<description><![CDATA[Here is an edit of the readme file found in the LocalNotification plugin for phonegap on Android. I've stumbled on a couple of issues which were not covered there and decided to make a version of the step through that worked for me. Assuming you already have a basic project setup (with a 2.0-ish ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>Here is an edit of the readme file found in the <a href="https://github.com/phonegap/phonegap-plugins/tree/master/Android/LocalNotification">LocalNotification plugin</a> for phonegap on Android. I&#8217;ve stumbled on a couple of issues which were not covered there and decided to make a version of the step through that worked for me. Assuming you already have a basic project setup (with a 2.0-ish cordova version), follow these steps:</p>
<ol>
<li>Get a basic phonegap project running for Android.</li>
<li>Add the LocalNotification.js file to your project js files and add a reference in your index.html file. If you are using phonegap 2.0.0 or above, edit it and replace all occurrences of <code class="codecolorer text default"><span class="text">PhoneGap</span></code> with <code class="codecolorer text default"><span class="text">cordova</span></code></li>
<li>Create a package &#8216;com.phonegap.plugin.localnotification&#8217; (in Eclipse, right click on the project, new > Package)</li>
<li>Copy all plugin java files in your project&#8217;s package (drag and drop the files)</li>
<li>Edit AlarmReceiver.java and add import your.project.name.R; in the end of the import list</li>
<li>Edit the LocalNotification.java file by replacing <code class="codecolorer text default"><span class="text">alarm = new AlarmHelper(this.ctx);</span></code> with <code class="codecolorer text default"><span class="text">alarm = new AlarmHelper(cordova.getContext());</span></code></li>
<li>Make sure the package reference in the first line of each of those java files matches the folder you actually placed them in.</li>
<li>Update your res/xml/plugins.xml file with the following line: <code class="codecolorer text default"><span class="text">&lt;plugin name=&quot;LocalNotification&quot; value=&quot;com.phonegap.plugin.localnotification.LocalNotification&quot; /&gt;</span></code></li>
<li>
    Add the following fragment in the AndroidManifest.xml inside the application tag:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;receiver android:name=&quot;com.phonegap.plugin.localnotification.AlarmReceiver&quot; &gt;<br />
&lt;/receiver&gt;<br />
<br />
&lt;receiver android:name=&quot;com.phonegap.plugin.localnotification.AlarmRestoreOnBoot&quot; &gt;<br />
&nbsp; &nbsp; &lt;intent-filter&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &lt;action android:name=&quot;android.intent.action.BOOT_COMPLETED&quot; /&gt;<br />
&nbsp; &nbsp; &lt;/intent-filter&gt;<br />
&lt;/receiver&gt;</div></div>
</li>
<li>Here is example where the notification is shown 3 minutes after the app is run:
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&nbsp; &lt;head&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot; src=&quot;cordova-2.0.0.js&quot;&gt;&lt;/script&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot; src=&quot;LocalNotification.js&quot;&gt;&lt;/script&gt;<br />
&nbsp; &lt;/head&gt;<br />
&nbsp; &lt;body&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; document.addEventListener(&quot;deviceready&quot;, appReady, false);<br />
<br />
&nbsp; &nbsp; &nbsp; function appReady() {<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; if (typeof plugins !== &quot;undefined&quot;) {<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var now = new Date();<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; now.setSeconds(now.getSeconds() + 90);<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; plugins.localNotification.add({<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; date : now,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message : &quot;Phonegap - Boooyyyaaaaah!\r\nUpyoass!&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ticker : &quot;Yeeeaaaaahhhh!!!&quot;,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeatDaily : false,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id : 4<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; &nbsp; &nbsp; }<br />
&nbsp; &nbsp; &nbsp; };<br />
<br />
&nbsp; &nbsp; &lt;/script&gt;<br />
&nbsp; &lt;/body&gt;<br />
&lt;/html&gt;</div></div>
</li>
<li>That&#8217;s it.</li>
</ol>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=298&amp;md5=1c113787acae7d962bb2fb1f30e8b7af" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/using-the-android-localnotification-plugin-for-phonegap/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A render of a Cibul event using the freshly published Cibul Plugin</title>
		<link>http://tech.cibul.net/a-render-of-a-cibul-event-using-the-freshly-published-cibul-plugin/</link>
		<comments>http://tech.cibul.net/a-render-of-a-cibul-event-using-the-freshly-published-cibul-plugin/#comments</comments>
		<pubDate>Fri, 20 Jul 2012 09:35:24 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=294</guid>
		<description><![CDATA[With this plugin, put links to cibul events in your posts and they will be converted into event renders like such:

La Java des Balkans

The template used is customizable, and the css can be modified in the plugin's admin page. Find the plugin page here.]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>With this plugin, put links to cibul events in your posts and they will be converted into event renders like such:</p>
<p><div class="cibul-event-list-item js_cibul_event_list_item">
  <div class="cibul-thumb">
  <a href="http://cibul.net/event/la-java-des-balkans*928" target="_blank"><img src="//cibul.s3.amazonaws.com/event_la-java-des-balkans*928_00.jpg"/></a>
</div>
<div class="cibul-content  with-thumb ">
  <div class="cibul-title"><a href="http://cibul.net/event/la-java-des-balkans*928" target="_blank">LA JAVA DES BALKANS</a></div>
  <div class="cibul-description"><a href="http://cibul.net/event/la-java-des-balkans*928" target="_blank">Pour cette dernière de la saison, DJ Tagada invite Aalma Dili à ouvrir le bal !</a></div>
  <div class="cibul-free-text"></div>
  <div class="cibul-spacetime"><a href="http://cibul.net/event/la-java-des-balkans*928" target="_blank">Saturday, July 28, in La Java</a><span class="cibul-map-icons js_cibul_map_icons"></span></div>
</div>  <ul class="cibul-locations js_cibul_location">
            <li>
        <span class="js_cibul_lat">48.870965</span>
        <span class="js_cibul_lng">2.373952</span>
        <span class="js_cibul_placename">La Java</span>
        <span class="js_cibul_address">105 Rue du Faubourg du Temple, 75010 Paris, France</span>
        <span class="js_cibul_slug">la-java</span>
      </li>
          </ul>
</div></p>
<p>The template used is customizable, and the css can be modified in the plugin&#8217;s admin page. Find the plugin page <a href="http://wordpress.org/extend/plugins/cibul-event-rendering/">here</a>.</p>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=294&amp;md5=9f3f767e4ab41c3ba8be4c2fc11fbf5f" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/a-render-of-a-cibul-event-using-the-freshly-published-cibul-plugin/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turn urls into links in text with jQuery</title>
		<link>http://tech.cibul.net/turn-urls-into-links-in-text-with-jquery/</link>
		<comments>http://tech.cibul.net/turn-urls-into-links-in-text-with-jquery/#comments</comments>
		<pubDate>Sat, 03 Dec 2011 14:11:33 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=263</guid>
		<description><![CDATA[Here is a little function that can be used to turn urls found in text on a page into clickable links. I assembled it and tweaked it taking bits and pieces from several forums.

See it running here


/* doLinks script */ 

function linkify(inputText, options) {
	
  this.options = {linkClass: 'url', targetBlank: true};

  this.options ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>Here is a little function that can be used to turn urls found in text on a page into clickable links. I assembled it and tweaked it taking bits and pieces from several forums.</p>
<p>See it running <a href="http://apps.cibul.net/dolinks/">here</a></p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">/* doLinks script */ <br />
<br />
function linkify(inputText, options) {<br />
&nbsp; &nbsp; <br />
&nbsp; this.options = {linkClass: 'url', targetBlank: true};<br />
<br />
&nbsp; this.options = $.extend(this.options, options);<br />
&nbsp; <br />
&nbsp; inputText = inputText.replace(/\u200B/g, &quot;&quot;);<br />
<br />
&nbsp; //URLs starting with http://, https://, or ftp://<br />
&nbsp; var replacePattern1 = /(src=&quot;|href=&quot;|&quot;&gt;|\s&gt;)?(https?|ftp):\/\/[-A-Z0-9+&amp;@#\/%?=~_|!:,.;ï]*[-A-Z0-9+&amp;@#\/%=~_|ï]/gim;<br />
&nbsp; var replacedText = inputText.replace(replacePattern1, function($0,$1){ return $1?$0:'&lt;a class=&quot;'+ this.options.linkClass + '&quot; href=&quot;' + $0 + '&quot;' + (this.options.targetBlank?'target=&quot;_blank&quot;':'') + '&gt;'+ $0+ '&lt;/a&gt;';});<br />
<br />
&nbsp; //URLS starting with www and not the above<br />
&nbsp; var replacePattern2 = /(src=&quot;|href=&quot;|&quot;&gt;|\s&gt;|https?:\/\/|ftp:\/\/)?www\.[-A-Z0-9+&amp;@#\/%?=~_|!:,.;ï]*[-A-Z0-9+&amp;@#\/%=~_|ï]/gim;<br />
&nbsp; var replacedText = replacedText.replace(replacePattern2, function($0,$1){ return $1?$0:'&lt;a class=&quot;'+ this.options.linkClass + '&quot; href=&quot;http://' + $0 + '&quot;' + (this.options.targetBlank?'target=&quot;_blank&quot;':'') + '&gt;'+ $0+ '&lt;/a&gt;';});<br />
<br />
&nbsp; //Change email addresses to mailto:: links<br />
&nbsp; var replacePattern3 = /([\.\w]+@[a-zA-Z_]+?\.[a-zA-Z]{2,6})/gim;<br />
&nbsp; var replacedText = replacedText.replace(replacePattern3, '&lt;a class=&quot;' + this.options.linkClass + '&quot; href=&quot;mailto:$1&quot;&gt;$1&lt;/a&gt;');<br />
<br />
&nbsp; return replacedText;<br />
}<br />
<br />
$.fn.doLinks = function(){<br />
&nbsp; this.each(function(){<br />
&nbsp; &nbsp; $(this).html(linkify($(this).html()));<br />
&nbsp; });<br />
}</div></div>
<p>Just apply .doLinks() on the elements you need to process. Let me know of links that don&#8217;t work with this.</p>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=263&amp;md5=0d7ac71ca7f42f0ae1194b657ccf0204" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/turn-urls-into-links-in-text-with-jquery/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Drag and Drop two Markers linked with a line using Google Maps v3</title>
		<link>http://tech.cibul.net/drag-and-drop-two-markers-linked-with-a-line-on-google-maps-v3/</link>
		<comments>http://tech.cibul.net/drag-and-drop-two-markers-linked-with-a-line-on-google-maps-v3/#comments</comments>
		<pubDate>Fri, 08 Jul 2011 09:42:36 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://tech.cibul.net/?p=240</guid>
		<description><![CDATA[In this article we'll use the google maps v3 API to show two draggable markers on a map linked with a line. As the markers are dragged, we want the line position to be updated so that it appears to always be linked and we also want to an updated indicator displaying the distance ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p>In this article we&#8217;ll use the google maps v3 API to show two draggable markers on a map linked with a line. As the markers are dragged, we want the line position to be updated so that it appears to always be linked and we also want to an updated indicator displaying the distance between the two markers in kilometers.</p>
<p>To achieve this, we will use the Markers <a href="http://code.google.com/intl/fr-FR/apis/maps/documentation/javascript/reference.html#MVCObject">MVCObject</a> properties to bind their positions to a method which will update the line and indicator as the Markers are dragged around the map.</p>
<h2>Getting started by creating a Map</h2>
<p>We start with an empty html page in which we will write our script. This page shows a full page map with over it a box in which we&#8217;ll keep an updated value of the distance between the markers. Here we go:</p>
<div class="codecolorer-container html4strict default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="html4strict codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #00bbdd;">&lt;!DOCTYPE html&gt;</span><br />
<span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/meta.html"><span style="color: #000000; font-weight: bold;">meta</span></a> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;content-type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span><span style="color: #66cc66;">/</span>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span>Distance widget<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/title.html"><span style="color: #000000; font-weight: bold;">title</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; html, body { height: 100%; margin: 0; padding:0; }<br />
&nbsp; &nbsp; &nbsp; #map-canvas { height: 100%; width: 100%; }<br />
&nbsp; &nbsp; &nbsp; #distance-display { position: absolute; bottom: 0; left: 0; width: 100px; height: 20px; z-index: 1; background: white; text-align: center; padding: 10px;}<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/style.html"><span style="color: #000000; font-weight: bold;">style</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://maps.google.com/maps/api/js?sensor=false&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://code.jquery.com/jquery-1.6.2.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a> <span style="color: #000066;">type</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span><br />
&nbsp; &nbsp; &nbsp; $(function(){<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; var map = new google.maps.Map($('#map-canvas'), {<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; center: new google.maps.LatLng(48.860932,2.335925),<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; zoom: 13,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mapTypeId: google.maps.MapTypeId.ROADMAP<br />
&nbsp; &nbsp; &nbsp; &nbsp; });<br />
<br />
&nbsp; &nbsp; &nbsp; });<br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">script</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/head.html"><span style="color: #000000; font-weight: bold;">head</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;distance-display&quot;</span>&gt;</span>distance<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&lt;<a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;map-canvas&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">div</span></a>&gt;</span><br />
&nbsp; <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/body.html"><span style="color: #000000; font-weight: bold;">body</span></a>&gt;</span><br />
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><a href="http://december.com/html/4/element/html.html"><span style="color: #000000; font-weight: bold;">html</span></a>&gt;</span></div></div>
<p>With this we have a working full page map ready to receive some markers and a polyline. The libraries we are using are the google maps v3 (obviously) and the jquery library which we use to handle things on the page (like $(&#8216;#map-canvas&#8217;)) and also to ensure that the javascript is not executed before the page is fully loaded (that is what the $(function(){}) is for).</p>
<h2>Add two markers and a line</h2>
<p>Now we add our two markers. One on the Louvre Museum and the second on the Eiffel Tower. Add this after the map declaration:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map<span style="color: #339933;">:</span> map<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position<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><span style="color: #CC0000;">48.860932</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2.335925</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; draggable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raiseOnDrag<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> marker2 <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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map<span style="color: #339933;">:</span> map<span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; position<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><span style="color: #CC0000;">48.858221</span><span style="color: #339933;">,</span><span style="color: #CC0000;">2.29449</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; draggable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raiseOnDrag<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>We also create the line, which is a <a href="http://code.google.com/intl/fr-FR/apis/maps/documentation/javascript/reference.html#Polyline">Polyline</a> with only two positions, one for each marker:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> line <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;">Polyline</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span>marker.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> marker2.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map<span style="color: #339933;">:</span> map<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<h2>Calculate the distance between two markers</h2>
<p>Before we start binding the markers with the line, we need to be able to calculate the distance between the markers. There used to be a practical method to do this in a previous version the the google maps API but it does not exist in the latest version (last time I checked anyways). So we will add it to the LatLng class so that we can easily determine the distance between two points. This methods uses the <a href="http://en.wikipedia.org/wiki/Haversine_formula">Haversine formula</a> to calculate the distance. Add it before the $(function(){&#8230; line:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; google.<span style="color: #660066;">maps</span>.<span style="color: #660066;">LatLng</span>.<span style="color: #660066;">prototype</span>.<span style="color: #660066;">distanceFrom</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>newLatLng<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>newLatLng<span style="color: #339933;">==</span>undefined<span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dLat <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>newLatLng.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">180</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> dLon <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>newLatLng.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lng</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">180</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>dLat<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>dLat<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">180</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">cos</span><span style="color: #009900;">&#40;</span>newLatLng.<span style="color: #660066;">lat</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">PI</span> <span style="color: #339933;">/</span> <span style="color: #CC0000;">180</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span> Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>dLon<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">sin</span><span style="color: #009900;">&#40;</span>dLon<span style="color: #339933;">/</span><span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">6371000</span> <span style="color: #339933;">*</span> <span style="color: #CC0000;">2</span> <span style="color: #339933;">*</span> Math.<span style="color: #660066;">atan2</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Math.<span style="color: #660066;">sqrt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">-</span>a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>With this, we can now simply determine the distance between two points. We apply it on the position of our markers at the end of the script:</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#distance-display'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>marker.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">distanceFrom</span><span style="color: #009900;">&#40;</span>marker2.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">100</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' km'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></div></div>
<p>Here we are calculating the distance, rounding it up to have something in kilometers and we put it in our display</p>
<h2>Tie everything up using MVCObject properties</h2>
<p>For the time being, everything is initialized, the two markers are displayed with a line in between them, the distance between the two markers is displayed as well at the bottom of the screen, but nothing happens when one of the markers is dragged. What we need is to update the position of the line and update the distance display as the drag occurs. This is where the MVCObject properties of our objects are useful. Using them, we can bind attributes (in our case, the position of the markers) to methods which will update the line position and the distance display any time the attributes are changed.
<p>First we create the method that will update the line and the display (before the $(function(){ again): </p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;">var</span> update_line_and_display <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> display<span style="color: #339933;">,</span> position1<span style="color: #339933;">,</span> position2<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// update the line</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; line.<span style="color: #660066;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> position1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; line.<span style="color: #660066;">getPath</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">setAt</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> position2<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #006600; font-style: italic;">// update the display</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #009900;">&#40;</span>display<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>Math.<span style="color: #660066;">round</span><span style="color: #009900;">&#40;</span>position1.<span style="color: #660066;">distanceFrom</span><span style="color: #009900;">&#40;</span>position2<span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #CC0000;">100</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">' km'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>We want that function to be called whenever the position of the markers change. As markers inherit the MVCObject, their attributes are bound to methods which if defined are triggered any time the attribute is changed. The names of these methods derive from the attribute. In our case, we use the position_changed method for each marker. We add this at the end of the script and we are done!</p>
<div class="codecolorer-container javascript default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="javascript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; &nbsp; &nbsp; marker.<span style="color: #660066;">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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update_line_and_display<span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> <span style="color: #3366CC;">'#distance-display'</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;">'position'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> marker2.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; marker2.<span style="color: #660066;">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><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; update_line_and_display<span style="color: #009900;">&#40;</span>line<span style="color: #339933;">,</span> <span style="color: #3366CC;">'#distance-display'</span><span style="color: #339933;">,</span> marker.<span style="color: #660066;">getPosition</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">'position'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;">&#125;</span></div></div>
<p>You can see it working <a href="http://apps.cibul.net/gmapruler/index.html">here</a>.</p>
 <p><a href="http://tech.cibul.net/?flattrss_redirect&amp;id=240&amp;md5=c7d651323b9670ddb25ad9bc69c490ba" title="Flattr" target="_blank"><img src="http://tech.cibul.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/drag-and-drop-two-markers-linked-with-a-line-on-google-maps-v3/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Install Maxmind&#8217;s GeoIP IP database on Ubuntu for PHP</title>
		<link>http://tech.cibul.net/install-maxminds-geoip-ip-database-on-ubuntu-for-php/</link>
		<comments>http://tech.cibul.net/install-maxminds-geoip-ip-database-on-ubuntu-for-php/#comments</comments>
		<pubDate>Tue, 19 Oct 2010 09:23:37 +0000</pubDate>
		<dc:creator>kaore</dc:creator>
				<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://tech.cibul.org/?p=133</guid>
		<description><![CDATA[Maxmind provides a useful database and API for server side IP Geolocation. It is useful when you don't want your server to hang on external services each time fetching a location from an IP address is needed. This posts goes through the installation process of the database with a PHP extension on an Ubuntu ...]]></description>
			<content:encoded><![CDATA[<p class="wp-flattr-button"></p><style type="text/css">.cibul-event-list-item { margin: 3em; }
.cibul-event-list-item a { color: inherit; text-decoration: none; }
.cibul-event-list-item > div { vertical-align: middle; text-align: center; }

.cibul-event-list-item .cibul-thumb { width: 250px; margin: 0 auto;  }
.cibul-event-list-item .cibul-thumb img { vertical-align: middle; width:100%; }
.cibul-event-list-item .cibul-content { font-size: 1.6em;  width: 250px; text-align: left; margin: 0 auto; }

.cibul-event-list-item .cibul-description, .cibul-event-list-item .cibul-spacetime { font-size: 0.8em; }

.cibul-event-list-item .cibul-map-icons { display: inline-block; padding: 0 0.5em; }
.cibul-event-list-item .cibul-map-icons img { vertical-align: middle;  padding: 0.1em; width: 1.1em; } .cibul-event-list-item .cibul-locations { display: none; }</style><p><a href="http://www.maxmind.com">Maxmind</a> provides a useful database and API for server side IP Geolocation. It is useful when you don&#8217;t want your server to hang on external services each time fetching a location from an IP address is needed. This posts goes through the installation process of the database with a PHP extension on an Ubuntu platform. I&#8217;m writing this for my own use. If it can be useful to you, yeepee.</p>
<p>See it working <a href="http://apps.cibul.org/maxmind/">here</a>. I&#8217;ll appreciate it if you can comment at the bottom of the page on how off from the result showed on the page is from your actual location.</p>
<p>Open a terminal</p>
<p>Download the C library in any location on your system (the latest as of today is <a href="http://geolite.maxmind.com/download/geoip/api/c/GeoIP-1.4.6.tar.gz">here</a>):</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">wget</span> http:<span style="color: #000000; font-weight: bold;">//</span>geolite.maxmind.com<span style="color: #000000; font-weight: bold;">/</span>download<span style="color: #000000; font-weight: bold;">/</span>geoip<span style="color: #000000; font-weight: bold;">/</span>api<span style="color: #000000; font-weight: bold;">/</span>c<span style="color: #000000; font-weight: bold;">/</span>GeoIP-1.4.6.tar.gz</div></div>
<p>Decompress it: <code class="codecolorer bash default"><span class="bash"><span style="color: #c20cb9; font-weight: bold;">tar</span> <span style="color: #660033;">-xvzf</span> GeoIP-1.4.6.tar.gz</span></code> and go into the folder to type the following: </p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> .<span style="color: #000000; font-weight: bold;">/</span>configure<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span><br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> check<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></div></div>
<p>Download the latest PHP extension (today its <a href="http://pecl.php.net/get/geoip-1.0.7.tgz">here</a>)</p>
<p>Decompress it, go in the geoip folder and type</p>
<div class="codecolorer-container bash default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #c20cb9; font-weight: bold;">sudo</span> phpize<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> .<span style="color: #000000; font-weight: bold;">/</span>configure<br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span><br />
<span style="color: #c20cb9; font-weight: bold;">sudo</span> <span style="color: #c20cb9; font-weight: bold;">make</span> <span style="color: #c20cb9; font-weight: bold;">install</span></div></div>
<p>Add the following line in your php.ini file used by apache (in my case: /etc/php5/apache2/php.ini): <code class="codecolorer bash default"><span class="bash"><span style="color: #007800;">extension</span>=geoip.so</span></code></p>
<p>To see if things have been working well until now, create a php test web page. We&#8217;ll make it prettier later on.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;html&gt;<br />
&nbsp; &lt;head&gt;<br />
&nbsp; &nbsp; &lt;title&gt;Testing Maxmind GeoLite&lt;/title&gt;<br />
&nbsp; &lt;/head&gt;<br />
&nbsp; &lt;body&gt;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <a href="http://www.php.net/var_dump"><span style="color: #990000;">var_dump</span></a><span style="color: #009900;">&#40;</span>geoip_db_get_all_info<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &lt;/body&gt;<br />
&lt;/html&gt;</div></div>
<p>To complete the installation, unzip and set <a href="http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz">GeoLiteCity</a> database in the folder indicated by the test page (here /usr/local/share/GeoIP/GeoIPCity.dat).</p>
<p>Once the database is downloaded and placed in the correct location, use the following functions to get the latitude or longitude from the ip address:</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000088;">$ip_location</span> <span style="color: #339933;">=</span> geoip_record_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$ip_location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$ip_location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></div></div>
<p>To wrap things up, the following code puts the retrieved latitude and longitude on a neat little google map. Paste this in your test page:
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&lt;html&gt;<br />
&nbsp; &lt;head&gt;<br />
&nbsp; &nbsp; &lt;title&gt;Testing Maxmind GeoLite&lt;/title&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot; src=&quot;http://maps.google.com/maps/api/js?sensor=false&quot;&gt;&lt;/script&gt;<br />
&nbsp; &nbsp; &lt;script type=&quot;text/javascript&quot;&gt;<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #000088;">$ip_location</span> <span style="color: #339933;">=</span> geoip_record_by_name<span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'REMOTE_ADDR'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; var initialize = function(){<br />
<br />
&nbsp; &nbsp; &nbsp; var map = new google.maps.Map(document.getElementById(&quot;map&quot;),{<br />
&nbsp; &nbsp; &nbsp; &nbsp; zoom: 13,<br />
&nbsp; &nbsp; &nbsp; &nbsp; center: new google.maps.LatLng(<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ip_location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>,<span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$ip_location</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span>),<br />
&nbsp; &nbsp; &nbsp; &nbsp; mapTypeId: google.maps.MapTypeId.ROADMAP<br />
&nbsp; &nbsp; &nbsp; });<br />
<br />
&nbsp; &nbsp; }<br />
<br />
&nbsp; &nbsp; &lt;/script&gt;<br />
&nbsp; &lt;/head&gt;<br />
&nbsp; &lt;body onload=&quot;initialize()&quot;&gt;<br />
&nbsp; &nbsp; &lt;h1&gt;IP Geolocation with Maxmind&lt;/h1&gt;<br />
&nbsp; &nbsp; &lt;div id=&quot;map&quot; style=&quot;width:300px; height:300px; margin: 10px;&quot;&gt;&lt;/div&gt;<br />
&nbsp; &lt;/body&gt;<br />
&lt;/html&gt;</div></div>
<p>Things should be working now.</p>
]]></content:encoded>
			<wfw:commentRss>http://tech.cibul.net/install-maxminds-geoip-ip-database-on-ubuntu-for-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->