我只是使用它:
navigator.geolocation.getCurrentPosition
适用于Chrome,Firefox等,但不适用于Safari。
我读过它只有在启用了Wifi时才能工作。
有没有办法让它在不需要Wifi的情况下工作?也许使用Modernizr?
使用AngularJS,这是我的确切代码:
$scope.showPosition = function (position) {
var lat = position.coords.latitude;
var lng = position.coords.longitude;
};
$scope.showError = function (error) {
switch (error.code) {
case error.PERMISSION_DENIED:
$scope.error = "User denied the request for Geolocation."
break;
case error.POSITION_UNAVAILABLE:
$scope.error = "Location information is unavailable."
break;
case error.TIMEOUT:
$scope.error = "The request to get user location timed out."
break;
case error.UNKNOWN_ERROR:
$scope.error = "An unknown error occurred."
break;
}
$scope.$apply();
};
$scope.getLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
}
else {
alert("Geolocation is not supported by this browser.");
}
};
$scope.getLocation();
不使用Wifi,抛出的错误是:Location information is unavailable.