<?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>Ben Werdmuller von Elgg &#187; javascript</title>
	<atom:link href="http://benwerd.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://benwerd.com</link>
	<description></description>
	<lastBuildDate>Wed, 08 Sep 2010 09:55:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Geolocation in HTML 5 and Javascript</title>
		<link>http://benwerd.com/2009/07/geolocation-in-html-5-and-javascript/</link>
		<comments>http://benwerd.com/2009/07/geolocation-in-html-5-and-javascript/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 17:42:23 +0000</pubDate>
		<dc:creator>Ben Werdmuller</dc:creator>
				<category><![CDATA[Software development]]></category>
		<category><![CDATA[geolocation]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[html 5]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://benwerd.com/2009/07/geolocation-in-html-5-and-javascript/</guid>
		<description><![CDATA[HTML 5 – as-yet unreleased, but shaping up well – contains a specification for finding the current location of the user. The API, if your browser supports it and you grant the web application access, returns your latitude, longitude, elevation, speed and some other details. (If your web-capable device doesn’t have GPS, these details will [...]]]></description>
			<content:encoded><![CDATA[<p>HTML 5 – as-yet unreleased, but shaping up well – contains a specification for finding the current location of the user. The API, if your browser supports it and you grant the web application access, returns your latitude, longitude, elevation, speed and some other details. (If your web-capable device doesn’t have GPS, these details will be estimated using your IP address and other factors.)</p>
<p>A couple of weeks ago, I created <a href="http://benwerd.com/lab/geo.php">a page to test this feature</a>. If your browser is geo capable, this will reveal exactly what data about your location is being sent to web applications that ask for it.</p>
<p>If you’re a developer, here’s how I created the page.</p>
<p> <span id="more-775"></span>
<p><strong>Developing for geolocation in Javascript</strong></p>
<p>The following all occurs in a Javascript code block towards the bottom of the page. You could also include this in the jQuery $() function, if you use that framework – the point is that it needs to execute once the page layout has been written to the browser.</p>
<p><strong>Testing for geolocation capability</strong></p>
<p>To test whether the browser has geolocation capabilities, we need to test for the existence of <code>navigator.geolocation</code>:</p>
<pre>if (navigator.geolocation) {
	// Execute geolocation code
} else {
	// Execute code for geolocation not available
}</pre>
<p><strong>Getting location data</strong></p>
<p>The actual function to call in order to get the location data is: <code>navigator.geolocation.getCurrentPosition(exportPosition, errorPosition);</code></p>
<p>Both <code>exportPosition</code> and <code>errorPosition</code> are functions that we must define. The former is called if the location is retrievable; the latter if it isn’t. (You can also omit the second parameter and neglect to have an error handler function.)</p>
<p>The <code>exportPosition</code> function takes one parameter: <code>position</code>. This in turn has a property, coords, which contains all of the major location data:</p>
<ul>
<li>position.coords.latitude: the latitude of the user </li>
<li>position.coords.longitude: the longitude of the user </li>
<li>position.coords.accuracy: a number representing the accuracy of the latitude and longitude (in metres) </li>
<li>position.coords.altitude: the altitude of the user in metres above sea level </li>
<li>position.coords.altitudeAccuracy: the accuracy of the above altitude reading, again in metres </li>
<li>position.coords.heading: the heading of the user, in degrees clockwise from north </li>
<li>position.coords.speed: the speed of the user in metres per second </li>
</ul>
<p>This can then be fed into the functionality of your choice.</p>
<p>For an example of how this can be used, have a look at the source of <a href="http://benwerd.com/lab/geo.php">my test page</a>. Browsers that are known to support geolocation include:</p>
<ul>
<li>Firefox 3.5</li>
<li>Safari in iPhone 3.0</li>
</ul>
<p>Because geolocation is included in the HTML 5 specification, most browsers will support this soon.</p>
<h3>Related entries</h3>
<ul class="related_post">
<li><a href="http://benwerd.com/2010/06/devices-and-desires-why-the-portable-device-wars-are-a-red-herring/" title="Devices and desires: why the portable device wars are a red herring">Devices and desires: why the portable device wars are a red herring (2)</a></li>
<li><a href="http://benwerd.com/2010/04/so-why-do-we-need-apps-anyway/" title="So why do we need apps anyway?">So why do we need apps anyway? (3)</a></li>
<li><a href="http://benwerd.com/2009/11/charging-for-software-in-the-age-of-web-apps/" title="Charging for software in the age of web apps">Charging for software in the age of web apps (2)</a></li>
</ul>
<br /><a href="http://benwerd.com/2009/07/geolocation-in-html-5-and-javascript/#comments" title="Comments on &quot;Geolocation in HTML 5 and Javascript&quot;"><img src="http://benwerd.com/wp-content/plugins/feed-comments-number/image.php?775" alt="Comments" /></a> <img src="http://benwerd.com/wp-content/plugins/feed-statistics.php?view=1&post_id=775" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://benwerd.com/2009/07/geolocation-in-html-5-and-javascript/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
