Skip to main content

Get GeoLocation using jQuery (API) GeoPlugin

By Sam Deering

JavaScript

Share:

Free JavaScript Book!

Write powerful, clean and maintainable JavaScript.

RRP $11.95

jQuery offers a great way to find out where your website visitors geographic location. With a simple jQuery GeoLocation API call you find out your website viewers Country, Region & City. You can also find out thier Longitude and Latitude values to hook upto Google Maps to show where they are viewing from. The great news is that this service is completely free! :)

I’ve included an alert box to help you get it working, you should see something like this:

geo-location-alert

How to use jQuery GeoLocation:

  1. Copy the jQuery code below into the section of your webpage
  2. Copy the HTML code below into the section of your webpage
  3. Watch it’s magic!

jQuery Code:


jQuery(document).ready(function($) {
	alert("Your location is: " + geoplugin_countryName() + ", " + geoplugin_region() + ", " + geoplugin_city());

	var country = geoplugin_countryName();
	$("#country").append("<option value='1' selected>"+country+"</option>");

	var zone = geoplugin_region();
	$("#zone").append("<option value='1' selected>"+zone+"</option>");

	var district = geoplugin_city();
	$("#district").append("<option value='1' selected>"+district+"</option>");
});

HTML Code:

Country

	  

Region

	  

City

	  

Demo

It could be useful for registration forms which require the user to fill in thier location details, this is a form that I have auto populate form select dropdowns using jQuery GeoLocation method above:

geo-location-form

You can view a live demo of this on signup form.

GeoLocation API Details

GeoPlugin offers the following features:

  • Geo Location services
  • Location Statistics
  • Currency Converter
  • Location codes, symbols & abbreviations
  • Latitude/Longitude to place or post code

Geo Location services are available in the following formats and programming languages:

  • Javascript
  • PHP
  • JSON
  • XML
  • ASP

Full details can be found here: http://www.geoplugin.com/webservices/javascript

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

New books out now!

Get practical advice to start your career in programming!


Master complex transitions, transformations and animations in CSS!

Latest Remote Jobs