使用 ng-map/angularjs-google-maps 在 javascript 中检索当前位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33166268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve current location in javascript using ng-map/angularjs-google-maps
提问by Thomas
How do I retrieve my current location in javascript using the angularjs-google-maps api by allenhwkim?
如何使用 allenhwkim 的 angularjs-google-maps api 在 javascript 中检索我的当前位置?
Currently I am able to access my current location through the html code
目前我可以通过 html 代码访问我当前的位置
<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:
html:
<input type="submit" Value="currentLocation" ng-click ="currentLocation()"/>
js:
js:
$scope.currentLocation = function(){
//how do i get current location?
};
thank you!
谢谢!
回答by Aishwat Singh
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);