JavaScript
Article

Get Geo Location with 2 lines of JavaScript

By Sam Deering

geo-location

This is how you can get a user geographical (geo) location using just 2 lines of JavaScript code. The first line loads the geo location JavaScript file and the second alerts the users location (inside a document ready which can be on one line).

The Code


Things to note:

  • Geo location provided is based on the ip address / location of your ISP.
  • You are reliant on the API service provided by geoplugin.com.

Run Directly In Firebug

jQuery(document).ready(function($) {
    jQuery.getScript('http://www.geoplugin.net/javascript.gp', function() 
{
    var country = geoplugin_countryName();
    var zone = geoplugin_region();
    var district = geoplugin_city();
    console.log("Your location is: " + country + ", " + zone + ", " + district);
});
});

Full List of Geo Location Properties

function geoplugin_city() { return 'Dobroyd Point';} 
function geoplugin_region() { return 'New South Wales';} 
function geoplugin_regionCode() { return '02';} 
function geoplugin_regionName() { return 'New South Wales';} 
function geoplugin_areaCode() { return '0';} 
function geoplugin_dmaCode() { return '0';} 
function geoplugin_countryCode() { return 'AU';} 
function geoplugin_countryName() { return 'Australia';} 
function geoplugin_continentCode() { return 'OC';} 
function geoplugin_latitude() { return '-33.873600';} 
function geoplugin_longitude() { return '151.144699';} 
function geoplugin_currencyCode() { return 'AUD';} 
function geoplugin_currencySymbol() { return '$';} 
function geoplugin_currencyConverter(amt, symbol) { 
	if (!amt) { return false; } 
	var converted = amt * 0.9587170632; 
	if (converted <0) { return false; } 
	if (symbol === false) { return Math.round(converted * 100)/100; } 
	else { return '&#36;'+(Math.round(converted * 100)/100);} 
	return false; 
}

Hello World Example


 
  
 
 
  
 

Integrate into a form: http://www.jquery4u.com/api-calls/geolocation-jquery-api-geoplugin/

More Info on the Plugin: http://www.geoplugin.com/webservices/javascript

  • Pingback: Elegant D » Get Geo Location with 2 lines of JavaScript | jQuery4u()

  • http://www.auxiliarfarmacia.es auxiliar farmacia

    Thanks for the tutorial! It works pretty well but it’s not very accurate with some data ^_*

  • chuch den

    That useful script, but how to get geo location time zone ?

    • http://jquery4u.com/ jQuery4u

      Hi Church,

      I have quickly put this together, not fully working but will give you a good start.

      Get the Location (country)

      Get the UTC offset for the Timezone should give you the local time for the geolocation.
      http://jsfiddle.net/pr5Gb/
      http://jsfiddle.net/Z5Phs/

      Sorry I don’t have much time at the moment to code this fully.
      Sam

  • Ashwin

    Thank You. Thank you very much.
    It works perfect. Thanks for the post.

  • Sanjib

    Thanks for the post. Is it possible to get Area?

  • Keefer

    Works like a charm. Thanks so much for posting this.

  • Noahdecoco

    This is a great and easy to use plugin. I have a question though, and I’m afraid of the answer. I need to detect if the user is visiting my site from the UAE, does that mean I have to search for every country in the UAE? Hope not! That’s going to be a LOT of work! How many countries are even there in the UAE?

  • tony_malony

    Hey, great tips! I have a quick question though.

    I have a mobile landing page that says “within 10 miles of your location”. So, how can I change “your location” to the actual city the smartphone user views my landing page from?

  • smoof_sm

    Any solution for finding geolocation through https websites? This script gets blocked on those.

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.