2015-10-16 55 views
3

如何使用allenhwkim的angularjs-google-maps API检索我在javascript中的当前位置?在javascript中使用ng-map/angularjs-google-maps检索当前位置

目前我能够通过HTML代码来访问我的当前位置

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

但是我希望实现的当前位置按钮,这将让我在地图的中央我当前的位置,每当我远离我当前位置本身。

HTML:

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

JS:

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

谢谢!

回答

8
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); 
+1

还值得通过空检查navigator.geolocation来检查浏览器是否支持地理定位。见http://www.w3schools.com/html/html5_geolocation.asp – amnesia

相关问题