Get current location using Google Maps API in AngularJS

Posted By : Dipak Kumar Singh | 13-Jun-2016

In this  blog I am going to explain how to get current location using Google map api in angularjs.The Geolocation API defines a high-level interface to location information associated only with the device  hosting the implementation ,such as latitude and longitude.

$scope.location = function () {
 $(document).ready(function () {
var currgeocoder;
 //Set geo location lat and long
    navigator.geolocation.getCurrentPosition(function (position, html5Error) {
                        var geo_loc = processGeolocationResult(position);
                        var currLatLong = geo_loc.split(",");
                        initializeCurrent(currLatLong[0], currLatLong[1]);
                    });
                    //Get geo location result
                    function processGeolocationResult(position) {
                        var html5Lat = position.coords.latitude; //Get latitude
                        var html5Lon = position.coords.longitude; //Get longitude
                        var html5TimeStamp = position.timestamp; //Get timestamp
                        var html5Accuracy = position.coords.accuracy; //Get accuracy in meters
                        return (html5Lat).toFixed(8) + ", " + (html5Lon).toFixed(8);
                    }

                   //Check value is present 
                    function initializeCurrent(latcurr, longcurr) {
                            var geocoder;
                            geocoder = new google.maps.Geocoder();
                            var latlng = new google.maps.LatLng(latcurr, longcurr);
                            dataFactory.saveLatLng(latcurr);
                            dataFactory.saveLongcurr(longcurr);
                            geocoder.geocode(
                                {'latLng': latlng}, 
                                function(results, status) {
                                    if (status == google.maps.GeocoderStatus.OK) {
                                            if (results[0]) {
                                                var add= results[0].formatted_address ;
                                                console.log("add ",add)
                                                var  value=add.split(",");

                                                var count=value.length;
                                                var country=value[count-1];
                                                var state=value[count-2];
                                                console.log("state ",state);
                                                var state1=state.replace(/\d+/g, '');
                                                console.log("state1 ",state1);
                                                var city=value[count-3];
                                                
                                                console.log("city name is: " + city,"state",state);

                                              }
                                            else  {
                                                alert("address not found");
                                            }
                                    }
                                     else {
                                        alert("Geocoder failed due to: " + status);
                                    }
                                }
                            );
                    });
          }


 

THANKS

About Author

Author Image
Dipak Kumar Singh

Dipak is a skilled HTML Developer, expertise in UI Development. Dipak likes watching movies and playing computer games.

Request for Proposal

Name is required

Comment is required

Sending message..