<commit_before>---
layout: post
title: 'HTML5 JavaScript GeoLocation Example'
tags:
  - api
  - code
  - example
  - geolocation
  - html5
  - javascript
  - tutorial

---

Geolocation is a very useful tool. So useful in fact that it's been added to the HTML5 specification.

What makes Gelocation so useful is how much the web is used on phone these days. Websites can now be applications on your phone. These web applications can be games, document editors, you name it!

So here's a quick intro on how to use the HTML5 Geolocation API.

{% highlight javascript %}
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success, error);
} else {
  error('not supported');
}
{% endhighlight %}
"success" and "error" in the getCurrentPosition() call are callback functions.

Here's an example of the success() function.
<code>function success(position) {
   document.write("Latitude: " + position.coords.latitude + " Longitude: " +     position.coords.longitude);
}</code>


Here's the full example.
<code>
function success(position) {
    document.write("Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude);
}

function error(msg) {
    console.dir(msg);
    alert(msg);
}

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(success, error);
} else {
    error('not supported');
}
</code><commit_msg>Reformat javascript<commit_after>---
layout: post
title: 'HTML5 JavaScript GeoLocation Example'
tags:
  - api
  - code
  - example
  - geolocation
  - html5
  - javascript
  - tutorial

---

Geolocation is a very useful tool. So useful in fact that it's been added to the HTML5 specification.

What makes Gelocation so useful is how much the web is used on phone these days. Websites can now be applications on your phone. These web applications can be games, document editors, you name it!

So here's a quick intro on how to use the HTML5 Geolocation API.

{% highlight javascript %}
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(success, error);
} else {
  error('not supported');
}
{% endhighlight %}

"success" and "error" in the getCurrentPosition() call are callback functions.

Here's an example of the success() function.
{% highlight javascript %}
function success(position) {
   document.write("Latitude: " + position.coords.latitude + " Longitude: " +     position.coords.longitude);
}
{% endhighlight %}


Here's the full example.
{% highlight javascript %}
function success(position) {
    document.write("Latitude: " + position.coords.latitude + " Longitude: " + position.coords.longitude);
}

function error(msg) {
    console.dir(msg);
    alert(msg);
}

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(success, error);
} else {
    error('not supported');
}
{% endhighlight %}
{'fraction_non_alphanumeric': 0.10925860234125577, 'fraction_numerical': 0.002837885775097552, 'mean_word_length': 4.099457504520796, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1459373', 'n_tokens_mistral': 870, 'n_tokens_neox': 823, 'n_words': 281}