使用 Ionic Cordova 的地理定位返回不正确的 Lat/Long 值
Geolocation Using Ionic Cordova Returning Incorrect Lat/Long Values
我在使用带有 Ionic 和 Cordova 框架的地理定位插件时遇到问题。
使用 Visual Studio Community 2015,Cordova CLI 版本:4.3.0 并将 org.apache.cordova.geolocation 插件添加到 VSC-2015
我的controller.js
.controller('MapCtrl', function ($scope) {
function onSuccess(position) {
console.log(position.timestamp);
console.log(position.coords.latitude + " " + position.coords.longitude);
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
})
将 google 地图添加到 index.html
<script src="https://maps.googleapis.com/maps/api/js?sensor=true&v=3.exp"></script>
向我的 Map.html 文件添加了地图
<div id="map" data-tap-disabled="true" map> </div>
问题是我总是得到一个固定值 Lat/Long
即 Lat = 43.465187 和 Long = -80.522372
这不是我在 Lat/Long 方面的正确地理位置。我需要在 Lat/Long
方面的当前地理位置
请帮我找出故障。
我也在浏览器上使用 Ripple -Nexus (Galaxy)。
感谢任何帮助...
谢谢,
我不确定你为什么得到固定的 lat/long 值。然而,要使用 Ionic 获取设备的位置,以下对我有用:
ionic.Platform.ready($scope.getLocation);
$scope.getLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
}
else {
console.log('Geolocation is not supported by this browser.');
}
}
$scope.showPosition = function (position) {
console.log(position.timestamp);
console.log(position.coords.latitude + ' ' + position.coords.longitude);
}
$scope.showError = function (error) {
switch (error.code) {
case error.PERMISSION_DENIED:
console.log('User denied the request for Geolocation.');
break;
case error.POSITION_UNAVAILABLE:
console.log('Location information is unavailable.');
break;
case error.TIMEOUT:
console.log('The request to get user location timed out.');
break;
case error.UNKNOWN_ERROR:
console.log('An unknown error occurred.');
break;
}
}
请注意调用:
ionic.Platform.ready($scope.getLocation);
非常重要,因为您只能保证在设备 ready/initialised 后获得准确的 GPS 定位。
从 https://github.com/apache/cordova-plugin-geolocation
下载最新版本的 GeoLocation 插件
然后将 $cordovaGeolocation 注入控制器。
代码如下:
.controller('HomeCtrl', function($scope,$cordovaGeolocation) {
$scope.getLocation = function() {
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
//these are your your lat long values
var lat = position.coords.latitude
var long = position.coords.longitude
}, function(err) {
// error
});
}
})
注意:设备需要连接到互联网或 wifi,并且还需要在设备中保持 GPS 位置开启以获得正确的 Lat/Long 值。
我在使用带有 Ionic 和 Cordova 框架的地理定位插件时遇到问题。
使用 Visual Studio Community 2015,Cordova CLI 版本:4.3.0 并将 org.apache.cordova.geolocation 插件添加到 VSC-2015
我的controller.js
.controller('MapCtrl', function ($scope) {
function onSuccess(position) {
console.log(position.timestamp);
console.log(position.coords.latitude + " " + position.coords.longitude);
}
function onError(error) {
alert('code: ' + error.code + '\n' +
'message: ' + error.message + '\n');
}
navigator.geolocation.getCurrentPosition(onSuccess, onError);
})
将 google 地图添加到 index.html
<script src="https://maps.googleapis.com/maps/api/js?sensor=true&v=3.exp"></script>
向我的 Map.html 文件添加了地图
<div id="map" data-tap-disabled="true" map> </div>
问题是我总是得到一个固定值 Lat/Long 即 Lat = 43.465187 和 Long = -80.522372
这不是我在 Lat/Long 方面的正确地理位置。我需要在 Lat/Long
方面的当前地理位置请帮我找出故障。
我也在浏览器上使用 Ripple -Nexus (Galaxy)。
感谢任何帮助... 谢谢,
我不确定你为什么得到固定的 lat/long 值。然而,要使用 Ionic 获取设备的位置,以下对我有用:
ionic.Platform.ready($scope.getLocation);
$scope.getLocation = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition($scope.showPosition, $scope.showError);
}
else {
console.log('Geolocation is not supported by this browser.');
}
}
$scope.showPosition = function (position) {
console.log(position.timestamp);
console.log(position.coords.latitude + ' ' + position.coords.longitude);
}
$scope.showError = function (error) {
switch (error.code) {
case error.PERMISSION_DENIED:
console.log('User denied the request for Geolocation.');
break;
case error.POSITION_UNAVAILABLE:
console.log('Location information is unavailable.');
break;
case error.TIMEOUT:
console.log('The request to get user location timed out.');
break;
case error.UNKNOWN_ERROR:
console.log('An unknown error occurred.');
break;
}
}
请注意调用:
ionic.Platform.ready($scope.getLocation);
非常重要,因为您只能保证在设备 ready/initialised 后获得准确的 GPS 定位。
从 https://github.com/apache/cordova-plugin-geolocation
下载最新版本的 GeoLocation 插件然后将 $cordovaGeolocation 注入控制器。 代码如下:
.controller('HomeCtrl', function($scope,$cordovaGeolocation) {
$scope.getLocation = function() {
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
//these are your your lat long values
var lat = position.coords.latitude
var long = position.coords.longitude
}, function(err) {
// error
});
}
})
注意:设备需要连接到互联网或 wifi,并且还需要在设备中保持 GPS 位置开启以获得正确的 Lat/Long 值。