page loader

How to get a visitors location using IP Geo-Location?

Understanding the location of your website visitors has become an important aspect of improving user experience and increasing conversion rates by creating geo targeted content. HTML5 geo-location offers a new an easy way for developers to detect user location rather accurately. This tutorial shows the basics of how to access a user location using Javascript.

Note: The following method is “intrusive” and will ask your visitor if they would like to share location information. See an example here . You can find the non intrusive way below the tutorial.


Finding out a user’s location can be done in a few simple steps using Javascript using the navigator.geolocation object available to you. Read below to see our commented code to understand how it works.

// Test to see if relocation is available

if (navigator.geolocation) {     

// Get the users current position               

  navigator.geolocation.getCurrentPosition(showPosition, showError, optn);

} else {

    alert(‘Geolocation is not supported in your browser’);


// Using the call back function to get access to the position objective

function showPosition(position) {

// Use the position object to access the coords

Latitude = position.coords.latitude;

Longitude = position.coords.longitude;


The above getCurrentPosition function has a success call back, error call back and position options. The success call back function provides a position object containing timestamp and coords. The coords object contains the most important information, latitude, longitude, accuracy, altitude, altitude accuracy, heading and speed. Using this information provided you will then be able to create changes based on the users coordinates.

As you can see, using HTML5 geolocation is extremely simple for those familiar with Javascript. There is plenty of reading available on this topic with a simple Google for “HTML5 Geo Location”.


#1 Geo Targeting SaaS

Geo Redirects

Geo Content

Geo Popups

Geo Images

Geo Notifications

Geo Links

Geo Javascript

Get Started For Free