Web Niraj
  • Facebook
  • Flickr
  • Github
  • Instagram
  • Linkedin
  • Twitter
  • YouTube
Online portfolio, code examples and developer blog
  • About
  • Contact
  • Portfolio
  • WordPress
  • Apps
    • Gemini Quota App for macOS
Search the site...
  • Home
  • Blog
  • Geolocation Using HTML5, jQuery and Google Maps

Geolocation Using HTML5, jQuery and Google Maps

2

Geolocating a user using their browser (if supported) is quite easy using HTML5 and JavaScript / jQuery. In fact, it can be done in under 50 lines of code. I wrote up this quick and easy tutorial to help anyone wanting to use geolocation within a browser.

If your browser supports geolocation, the below code will give you a simple static Google Maps image showing your location:

geolocation-complete

The key to the entire tutorial is the below code. It’s what gets the browser to locate the user:

if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
error('not supported');
}
view raw geolocation_2.js hosted with ❤ by GitHub

In Chrome, the browser will prompt the user to allow permission to access their location:

browser-geolocation-permission

The HTML:

<div id="wrapper">
<!-- Page heading -->
<h1>Geolocation</h1>
<!-- Status -->
<p>Finding your location: <span id="status">checking...</span></p>
</div>
view raw geolocation_1.html hosted with ❤ by GitHub

The JavaScript:

function success(position) {
// variable to store the coordinates
var location = position.coords.latitude + ',' + position.coords.longitude;
// select the span with id status
var s = $('#status');
// update the status message
s.html('found you!');
// Google Maps image using location
$('#wrapper').append('<img src="http://maps.google.com/maps/api/staticmap?center='+ location +
'&zoom=17&markers=size:medium|color:red|'+ location +
'&path=color:0x0000FF80|weight:2|'+ location +
'&size=480x360&sensor=false" />');
}
function error(msg) {
// select the span with id status
var s = $('#status');
// set the error message
s.html(typeof msg == 'string' ? msg : "failed");
}
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
error('not supported');
}
view raw geolocation_full.js hosted with ❤ by GitHub

Demo:

View a working demo of the this on jsbin. This is an exact copy of the code used above, and you can use jsbin to make clone of the code so you can edit it at your leisure. The code has been tested on Chrome, Firefox and Safari (on a Mac, make sure Location Services is enabled from System Preferences > Security & Privacy > Privacy tab).

Geolocation, HTML5, JavaScript, jQuery, Tutorial

2 comments on “Geolocation Using HTML5, jQuery and Google Maps”

  1. Justin says:
    February 14, 2013 at 3:14 PM

    This doesn’t work. What did you see this work on? I haven’t found one that works at all yet. I’ve been checking high and low.

    Reply
  2. Niraj Shah says:
    March 4, 2013 at 12:09 PM

    What browser did you test on? This example works on iOS, Android, Chrome, Safari and Firefox. Try out the code here: http://jsbin.com/egarin/latest

    You need to make sure you are including the jQuery library too. If you got any JavaScript errors, please post them here so I can take a look.

    Reply

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

StackExchange / StackOverflow

profile for Niraj Shah on Stack Exchange, a network of free, community-driven Q&A sites

Subscribe for updates

Support Me

Buy Me a Coffee

PSN Profile

Tags

ACL Amazon Web Services Android Android 4.4 KitKat Android 5.0 Lollipop Apache Backup Bug Command Line Cordova cPanel / WHM Facebook Facebook Graph API Facebook PHP SDK 4.0 Facebook Social Plugins Fan Page Flash Flat File Geolocation Google Nexus 5 Hacking HTML5 iOS JavaScript jQuery Laravel 5 Linux Mac OS MongoDB NodeJS Parse PDF PHP Plugin Portfolio PS4 Review Security Server SSH SSL Sysadmin Tutorial WordPress WordPress Plugins

© 2011-2026 Niraj Shah
  • Blog
  • Portfolio
  • WordPress
  • About Me
  • Contact Me
  • Privacy Policy