Permalink
Browse files

added locator.js to get user's location

  • Loading branch information...
1 parent 207b9ab commit b95d1275cbcee6a6c702af2ecd9784360596ab92 @marco-gallardo marco-gallardo committed Mar 31, 2012
Showing with 54 additions and 0 deletions.
  1. +54 −0 public/javascripts/locator.js
@@ -0,0 +1,54 @@
+$(document).ready(function() {
+
+ function errorGettingPosition(err)
+ {
+ if(err.code==1)
+ {
+ alert("User denied geolocation.");
+ }
+ else if(err.code==2)
+ {
+ alert("Position unavailable.");
+ }
+ else if(err.code==3)
+ {
+ alert("Timeout expired.");
+ }
+ else
+ {
+ alert("ERROR:"+ err.message);
+ }
+ }
+
+ function getAddress(coords) {
+ latlng = new google.maps.LatLng(coords.latitude, coords.longitude);
+ geocoder.geocode({'latLng': latlng}, function(results, status) {
+ if (status == google.maps.GeocoderStatus.OK) {
+ if (results[0]) {
+ $('#publisher_textarea_wrapper').append('<div id="location">' + results[0].formatted_address + '</div>');
+ }
+ } else {
+ alert("Geocoder failed due to: " + status);
+ }
+ })
+ }
+
+ function success(position) {
+ var s = document.querySelector('#location');
+
+ getAddress(position.coords);
+ }
+
+ function error(msg) {
+ var s = document.querySelector('#status');
+ s.className = 'fail';
+ errorGettingPosition(msg);
+ }
+
+ $("#locator").click(function(){
+ geocoder = new google.maps.Geocoder();
+ $('#publisher_textarea_wrapper').append('<div id="location"></div>')
+ navigator.geolocation.getCurrentPosition(success, error);
+ });
+
+});

0 comments on commit b95d127

Please sign in to comment.