Find User Location using HTML5 Geolocation..and Google Geocode for address

by Yamini Sontakke

HTML

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&amp;region=US&amp;language=en"></script>
<div id="results"></div>

JavaScript

var userLocation = (function() {

    return {

        init: function() {
            this.getLocation();
        },

        getLocation: function() {
            var self = this;
            navigator.geolocation.getCurrentPosition(self.geocodeLocation, self.printError);
        },

        geocodeLocation: function(loc) {
            var self = userLocation;
            self.position = {
                lat: loc.coords.latitude,
                long: loc.coords.longitude
            }
            self.geocoder = new google.maps.Geocoder();

            var currentLocation = new google.maps.LatLng(self.position.lat, self.position.long);

            self.geocoder.geocode({
                'latLng': currentLocation
            }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    self.address = results[2].formatted_address;
                    self.printLocation();
                }
            });
        },

        printLocation: function() {
            var self = userLocation;
            $('#results').append('Estimated address: ' + self.address + '<br>Estimated latlng: ' + self.position.lat + ', ' + self.position.long);
        },

        printError: function() {
            $('#results').append('No location found');
        }
    };

}());

$(document).ready(function() {
    userLocation.init();
});