geoPlugin Home


Some ideas of how to use geoPlugin web services

Below are just a few examples1) using geoPlugin's geolocation webservice, with detailed code for reproduction of how you can use the values returned from geoPlugin.
For sure, the things you can do with these geolocalized data are pretty much limitless, but here are just a few examples to give you some ideas.

If you have other examples, submit them to us and once verified, we will post them here (of course with credits).

Example 1: Simple Hello World

<html>
 <head>
  <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
 </head>
 <body>
  <script language="Javascript"> 
	document.write("Welcome to our visitors from "+geoplugin_city()+", "+geoplugin_countryName()); 
  </script>
 </body>
</html>







Example 2: Buying a television

<?php
$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
 
if ( $geoPlugin_array['geoplugin_currencyCode'] == 'USD' ) { //let's use a different base currency
 
	$geoPlugin_array = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR'] . '&base_currency=EUR') );
 
	echo '<h3>A &#8364;800 television from Germany will cost you '.$geoPlugin_array['geoplugin_currencySymbol'] . round( (800 * $geoPlugin_array['geoplugin_currencyConverter']),0) . '</h3>';
 
} else {
 
	echo '<h3>A $800 television from the US will cost you ' . $geoPlugin_array['geoplugin_currencySymbol'] . round( (800 * $geoPlugin_array['geoplugin_currencyConverter']),0) . '</h3>';
 
}
?>


A €800 television from Germany will cost you $1014



Example 3: Your own personalized weather forecast

Using the webservice from the Weather Underground, we can easily provide a personalized geolocated weather forecast for our visitors.

This code requires the ParseXml class.

<?php
 
$geoplugin = unserialize( file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $_SERVER['REMOTE_ADDR']) );
 
if ( is_numeric($geoplugin['geoplugin_latitude']) && is_numeric($geoplugin['geoplugin_longitude']) ) {
 
	$lat = $geoplugin['geoplugin_latitude'];
	$long = $geoplugin['geoplugin_longitude'];
	//set farenheight for US
	if ($geoplugin['geoplugin_countryCode'] == 'US') {
		$tempScale = 'fahrenheit';
		$tempUnit = '&deg;F';
	} else {
		$tempScale = 'celsius';
		$tempUnit = '&deg;C';
	}
	require_once('ParseXml.class.php');
 
	$xml = new ParseXml(); 
	$xml->LoadRemote("http://api.wunderground.com/auto/wui/geo/ForecastXML/index.xml?query={$lat},{$long}", 3);
	$dataArray = $xml->ToArray();
 
	$html = "<center><h2>Weather forecast for " . $geoplugin['geoplugin_city'];
	$html .= "</h2><table cellpadding=5 cellspacing=10><tr>";
 
	foreach ($dataArray['simpleforecast']['forecastday'] as $arr) {
 
		$html .= "<td align='center'>" . $arr['date']['weekday'] . "<br />";
		$html .= "<img src='http://icons-pe.wxug.com/i/c/a/" . $arr['icon'] . ".gif' border=0 /><br />";
		$html .= "<font color='red'>" . $arr['high'][$tempScale] . $tempUnit . " </font>";
		$html .= "<font color='blue'>" . $arr['low'][$tempScale] . $tempUnit . "</font>";
		$html .= "</td>";
 
 
	}
	$html .= "</tr></table>";
 
	echo $html;
}
 
?>


will output your own weather forecast.

Weather forecast for Ashburn

Saturday

72°F 48°F
Sunday

68°F 41°F
Monday

70°F 48°F
Tuesday

79°F 54°F
Wednesday

70°F 45°F
Thursday

63°F 41°F



Example 4: Embedding a Google Map

This is a geolocation example to embed a map. The example uses Google maps and as such requires your own free Google maps API key

<html>
 <head>
  <script src="http://maps.google.com/maps?file=api&v=2&key=Your_Google_Maps_API_Key" type="text/javascript"></script>
  <script language="JavaScript" src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
 
  <script type="text/javascript">
    function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(geoplugin_latitude(), geoplugin_longitude()), 12);
      }
    }
  </script>
 </head>
 <body>
   <div id="map" style="width: 500px; height: 300px"></div>
   <script>load();</script>
 </body>
</html>




Example 5: An AJAX Currency Converter

Normally, AJAX can't be used to perform cross-site requests, but using jQuery's JSON request, we can sidestep this annoyance.
This example, while not geolocation, stemmed from the currency converter that is included in the geolocation webservice.
The following currency converter example uses Google's Javascript API as the source for jQuery, and geoPlugin for the currency converter javascript. Of course, you may substitute these for your own versions.

 
<script src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
<!-- when using Google to load JSON API -->
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script src="http://www.geoplugin.net/ajax_currency_converter.gp" type="text/javascript"></script>
<!-- When using your own JSON API -->
<!-- <script src="http://www.geoplugin.net/ajax_currency_converter.gp?nogoog=1" type="text/javascript">
</script> -->
<input type='text' id='gp_amount' size='4' /> 
<select id="gp_from"></select> to <select id="gp_to"></select>
<p><input type='button' onClick='gp_convertIt()' value = 'Convert It' /></p>
<div id="gp_converted"></div>
<script>gp_currencySymbols()</script>


That's all that is needed to embed an AJAX real-time currency converter like below on your pages!

Convert to



Submit your own examples

Geolocation is very powerful and what you can do with it is practically limitless.

Do you have any geolocation examples that use geoPlugin that you want to share with the world? If so, submit them to us and once verified, we will post them here (of course with credits).

geoPlugin is all about helping webmasters in the simplest way with geolocation, so share your ideas with others. Currently supported languages for examples are Javascript, JSON, PHP, and XML.



Help geoPlugin grow

For geoPlugin to grow, we need to spread the word of this service.
All that we ask in return for us helping you, is that you add some recognition of geoPlugin in your pages, by adding the following unobtrusive HTML to the foot of your pages:

<a href="http://www.geoplugin.com/" target="_new" title="geoPlugin for IP geolocation">Geolocation by geoPlugin</a>

Geolocation by geoPlugin


Support geoPlugin and keep this service free!


See also

1) This product includes GeoLite data created by MaxMind, available from http://maxmind.com/
 
 
Bookmark with:    Digg    StumbleUpon    Del.icio.us    Technorati


Acceptable Use Policy    Contact geoPlugin