Retrieve current location in javascript using ng-map/angularjs-google-maps

11,915
var options = {
                enableHighAccuracy: true
            };

navigator.geolocation.getCurrentPosition(function(pos) {
                $scope.position = new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude);
                console.log(JSON.stringify($scope.position));                  
            }, 
            function(error) {                    
                alert('Unable to get location: ' + error.message);
            }, options);
Share:
11,915
Thomas
Author by

Thomas

Updated on July 26, 2022

Comments

  • Thomas
    Thomas almost 2 years

    How do I retrieve my current location in javascript using the angularjs-google-maps api by allenhwkim?

    Currently I am able to access my current location through the html code

    <marker animation="DROP" position="current-location"></marker>
    

    however I wish to implement a current location button which will let me center the map to my current location whenever I am away from my current location itself.

    html:

    <input type="submit" Value="currentLocation" ng-click ="currentLocation()"/>
    

    js:

    $scope.currentLocation = function(){
            //how do i get current location?
    };
    

    thank you!

  • amnesia
    amnesia about 8 years
    It's also worth checking that the browser supports geolocation first by null checking navigator.geolocation. See w3schools.com/html/html5_geolocation.asp