RSS

Tag Archives: geo data

HTML5 Geolocation

Demo              Download

<!DOCTYPE html>
<html>
	<head>

		<script src="<a href="view-source:http://maps.googleapis.com/maps/api/js?sensor=false">http://maps.googleapis.com/maps/api/js?sensor=false</a>"></script>
		<script>
			// Integration with google maps
			function loadMap(lat, lng) {
				var latlng = new google.maps.LatLng(lat, lng);

				var settings = {
					zoom: 14,
					center: latlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};

				var map = new google.maps.Map(document.getElementById('map_canvas'), settings);

				var marker = new google.maps.Marker({
					position: latlng,
					map: map,
					title: 'Your Position!'
				});

				document.getElementById('status').innerHTML = 'Position Found!';
			}

			// Initialize geolocation
			function initialize() {
				if (navigator.geolocation) {
					document.getElementById('status').innerHTML = 'Checking...';

					navigator.geolocation.getCurrentPosition(
						onSuccess,
						onError, {
							enableHighAccuracy: true,
							timeout: 20000,
							maximumAge: 120000
						});
				}
				else {
					document.getElementById('status').innerHTML = 'Geolocation not supported.';
				}
			}

			// Map position retrieved successfully
			function onSuccess(position) {
				var data = '';

				data += 'latitude: ' + position.coords.latitude + '<br/>';
				data += 'longitude: ' + position.coords.longitude + '<br/>';
				data += 'altitude: ' + position.coords.altitude + '<br/>';
				data += 'accuracy: ' + position.coords.accuracy + '<br/>';
				data += 'altitudeAccuracy: ' + position.coords.altitudeAccuracy + '<br/>';
				data += 'heading: ' + position.coords.heading + '<br/>';
				data += 'speed: ' + position.coords.speed + '<br/>';

				document.getElementById('data').innerHTML = data;

				loadMap(position.coords.latitude, position.coords.longitude);
			}

			// Error handler
			function onError(err) {
				var message;

				switch (err.code) {
					case 0:
						message = 'Unknown error: ' + err.message;
						break;
					case 1:
						message = 'You denied permission to retrieve a position.';
						break;
					case 2:
						message = 'The browser was unable to determine a position: ' + error.message;
						break;
					case 3:
						message = 'The browser timed out before retrieving the position.';
						break;
				}

				document.getElementById('status').innerHTML = message;
			}
		</script>
	</head>
	<body onload="<a>initialize()</a>">
		<div id="<a>status</a>"></div>
		<div id="<a>data</a>"></div>
		<div id="<a>map_canvas</a>" style="<a>width: 640px; height: 480px</a>"></div>
	</body>
</html>

 
1 Comment

Posted by on April 19, 2012 in Geo Programming, JavaScript

 

Tags: , , ,

Display user geo location data with Google Map API or Maxmind

First try to get geo data with Google JSApi. If it fails then try to connect with Maxmind database and retrieve data. This is a fallback option

<!DOCTYPE>
<html>
<head>
<title>Geo data</title>
<script src="http://maps.google.com/maps?file=api&v=2&amp;sensor=true" type="text/javascript"></script>
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
<script language="JavaScript" src="http://j.maxmind.com/app/geoip.js"></script>
<script>
google.load("jquery", "1.4.4");
</script>
</head>

<body>
<script type="text/javascript">
$(document).ready(function(){
if(google.loader.ClientLocation) {
// Google has found you
visitor_lat = google.loader.ClientLocation.latitude;
visitor_lon = google.loader.ClientLocation.longitude;
visitor_city = google.loader.ClientLocation.address.city;
visitor_region = google.loader.ClientLocation.address.region;
visitor_country = google.loader.ClientLocation.address.country;
visitor_countrycode = google.loader.ClientLocation.address.country_code;

$("#user_method").html("Google Geo");
$("#user_latlong").html(visitor_lat + " / " + visitor_lon);
$("#user_town").html(visitor_city); $("#user_county").html(visitor_region);
$("#user_country").html(visitor_country + " (" + visitor_countrycode + ")");
}
else {
// Google couldnt find you, Maxmind could
visitor_lat = geoip_latitude();
visitor_lon = geoip_longitude();
visitor_city = geoip_city();
visitor_region_code = geoip_region();
visitor_region = geoip_region_name();
visitor_country = geoip_country_name();
visitor_countrycode = geoip_country_code();
visitor_postcode = geoip_postal_code();

$("#user_method").html("MaxMind");
$("#user_latlong").html(visitor_lat + " / " + visitor_lon);
$("#user_postcode").html(visitor_postcode);
$("#user_town").html(visitor_city); $("#user_county").html(visitor_region + " ("+visitor_region_code+")");
$("#user_country").html(visitor_country + " (" + visitor_countrycode + ")");
}

if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
var latlng = new GLatLng(visitor_lat, visitor_lon);
map.setCenter(latlng, 13);
map.addOverlay(new GMarker(latlng));

var center = latlng;
var radius = 0.65;

//convert kilometers to miles-diameter
var radius = radius*1.609344;
var latOffset = 0.01;
var lonOffset = 0.01;
var latConv = center.distanceFrom(new GLatLng(center.lat()+0.1, center.lng()))/100;
var lngConv = center.distanceFrom(new GLatLng(center.lat(), center.lng()+0.1))/100;
// nodes = number of points to create circle polygon
var nodes = 40;
//Loop
var points = [];
var step = parseInt(360/nodes)||10;
for(var i=0; i<=360; i+=step) {
var pint = new GLatLng(center.lat() + (radius/latConv * Math.cos(i * Math.PI/180)), center.lng() + (radius/lngConv * Math.sin(i * Math.PI/ 180)));
// push pints into points array
points.push(pint);
}
var polygon = new GPolygon(points, "#f33f00", 1, 1, "#ff0000", 0.1);
map.addOverlay(polygon);
}
});
</script>
</body>
<div id="geoData">
<table style="width: 560px;" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td>Geocode Method:</td>
<td><span id="user_method"> </span></td>
</tr>
<tr>
<td>Your Latitude/Longitude:</td>
<td><span id="user_latlong"> </span></td>
</tr>
<tr>
<td>Town / City:</td>
<td><span id="user_town"> </span></td>

</tr>
<tr>
<td>Post Code:</td>
<td><span id="user_postcode"> </span></td>
</tr>
<tr>
<td>Approximate County:</td>
<td><span id="user_county"> </span></td>
</tr>
<tr>
<td>Country and Code:</td>
<td><span id="user_country"> </span></td>

</tr>
<tr>
<td colspan="2">
<div id="map_canvas" style=" margin:10px; border:1px solid #007; width: 540px; height: 360px;">Enable Javascript to view this map</div></td>
</tr>
</tbody>
</table>
</div>


</html>


 
6 Comments

Posted by on April 2, 2012 in Geo Programming, JavaScript

 

Tags: , , , ,