Last active
August 29, 2015 14:02
-
-
Save up1/32bdc695eceaf5f6355e to your computer and use it in GitHub Desktop.
Demo :: Javascript get Location
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 () { | |
$.when(getPosition()) | |
.then(lookupCountry) | |
.done(displayResults) | |
.fail(displayError) | |
}); |
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
var getPosition = function (options) { | |
var deferred = $.Deferred(); | |
navigator.geolocation.getCurrentPosition( | |
deferred.resolve, | |
deferred.reject, | |
options); | |
return deferred.promise(); | |
}; | |
var lookupCountry = function (position) { | |
var deferred = $.Deferred(); | |
var latlng = new google.maps.LatLng( | |
position.coords.latitude, | |
position.coords.longitude); | |
var geoCoder = new google.maps.Geocoder(); | |
geoCoder.geocode({ location: latlng }, deferred.resolve); | |
return deferred.promise(); | |
}; |
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
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> | |
<script> | |
(function () { | |
var getPosition = function (options) { | |
navigator.geolocation.getCurrentPosition( | |
lookupCountry, | |
null, | |
options); | |
}; | |
var lookupCountry = function (position) { | |
console.log(position); | |
var latlng = new google.maps.LatLng( | |
position.coords.latitude, | |
position.coords.longitude); | |
var geoCoder = new google.maps.Geocoder(); | |
geoCoder.geocode({ location: latlng }, displayResults); | |
}; | |
var displayResults = function (results, status) { | |
$("body").append("<div>").text(results[0].formatted_address); | |
}; | |
$(function () { | |
getPosition(); | |
}); | |
} ()); | |
</script> |
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
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> | |
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> | |
<script> | |
(function() { | |
var getPosition = function (options) { | |
var deferred = $.Deferred(); | |
navigator.geolocation.getCurrentPosition( | |
deferred.resolve, | |
deferred.reject, | |
options); | |
return deferred.promise(); | |
}; | |
var lookupCountry = function (position) { | |
var deferred = $.Deferred(); | |
var latlng = new google.maps.LatLng( | |
position.coords.latitude, | |
position.coords.longitude); | |
var geoCoder = new google.maps.Geocoder(); | |
geoCoder.geocode({ location: latlng }, deferred.resolve); | |
return deferred.promise(); | |
}; | |
var displayResults = function (results, status) { | |
$("body").append("<div>").text(results[0].formatted_address); | |
}; | |
var displayError = function (error) { | |
$("body").append("<div>").text("ERROR NAJAA : " + error.message); | |
}; | |
$(function () { | |
$.when(getPosition()) | |
.then(lookupCountry) | |
.done(displayResults) | |
.fail(displayError) | |
}); | |
}()); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment