2015-10-16 56 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

相關問題