1.安装定位插件
cmd切换至项目目录执行
cordova plugin add org.apache.cordova.geolocation
2.例子
$scope.getGeolocation = function() {
function onSuccess(position) {
console.log(
'Latitude纬度: ' + position.coords.latitude + '\n' +
'Longitude经度: ' + position.coords.longitude + '\n' +
'Altitude海拔: ' + position.coords.altitude + '\n' +
'Accuracy精度: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
$scope.latitude = position.coords.latitude;//纬度
$scope.longitude = position.coords.longitude;//经度
};
function onError(error) {
console.log('code: ' + error.code + '\n' + 'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
$scope.getGeolocation();
执行$scope.getGeolocation(),当前纬度经度已赋值给$scope.latitud 和 $scope.longitude