Created
October 20, 2012 16:22
-
-
Save Pushplaybang/3923823 to your computer and use it in GitHub Desktop.
Basic Geolocation with reverse geo coding for region
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bangGeoLocate() { | |
var Mylat; | |
var Mylng; | |
var geocoder; | |
function testGeo() | |
{ | |
if(navigator.geolocation ) | |
{ | |
navigator.geolocation.getCurrentPosition( success, fail ); | |
} | |
else | |
{ | |
alert("Sorry, your browser does not support geolocation services."); | |
} | |
} | |
// html5 geolocation supported | |
function success(position) { | |
// GeoPosition Object | |
alert("Your coordinates are " + position.coords.latitude + ", " + position.coords.longitude); | |
Mylat = position.coords.latitude; | |
Mylng = position.coords.longitude; | |
geoInfoDisplay(Mylat, Mylng); | |
} | |
// html5 geolocation not supported | |
function fail() { | |
alert("fuck you then."); | |
} | |
// Reverse Geo Code Lat Long to get address | |
function geoInfoDisplay(latparam, lngparam) { | |
var geocoder = new google.maps.Geocoder(); | |
// turn coordinates into an object | |
var yourLocation = new google.maps.LatLng(latparam, lngparam); | |
geocoder.geocode({ 'latLng': yourLocation }, function (results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
if (results[0]) { | |
$('body').append('<p>Your Region<br />' + | |
results[0].address_components[7].long_name + '</p>'); // nb - component 7 seems to return the region | |
console.log(results[0].address_components); | |
} else { | |
error('Google did not return any results.'); | |
} | |
} else { | |
error("Reverse Geocoding failed due to: " + status); | |
} | |
}); | |
} // end geocoding function | |
//Runs the Test which executes the subsequent functions | |
testGeo(); | |
} // end locate function | |
// requires <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> to be called prior to this function. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment