IOS PhoneGap Build 6.3.0 地理定位 getCurrentPosition 变慢
PhoneGap Build 6.3.0 geolocation getCurrentPosition slow on IOS
我是 PhoneGap Build 开发新手。在测试一些基本的应用程序功能时,我在 Iphone 6 / IOS 10 / PhoneGap Build 6.3.0.
上执行简单的地理定位请求时遇到超时问题
重新安装应用程序后,我启动它并通过 onclick -> geolocation() 启动地理定位。
只有当我将应用程序转到后台时,我才会收到允许位置请求的 IOS 请求(应该在我第一次执行 onclick -> 地理定位时出现,同时将应用程序置于前台)。
有时我会在很长时间后得到地理定位结果,有时却得不到。我已经在三个 PositionOptions 上尝试了所有可能的组合。
当我询问 Google 地图应用程序时,它会立即显示位置。
知道我做错了什么吗?
谢谢你,金
function do_geolocation(){
alert('do geoloc');
navigator.geolocation.getCurrentPosition(geo_onSuccess, geo_onError, {maximumAge:120000, enableHighAccuracy:false} );
}
onclick=do_geolocation();
function geo_onSuccess(position){
alert('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');
var arr = new Array();
arr['lat'] = position.coords.latitude;
arr['lng'] = position.coords.longitude;
var x = new Date();var cb = x.getTime();
}
function geo_onError(position){
alert('code: '+error.code+'\nmessage: '+error.message+'\n');
return false;
}
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "de.vvvvvv.secapp"
versionCode = "10"
version = "1.0.0" >
<!-- versionCode is optional and Android only -->
<name>vvvvvvv</name>
<description>
vvvvvvvvvvvv
</description>
<author href="http://vvvvvvv.de" email="info@vvvvvv.de">
Kim
</author>
<plugin name="cordova-plugin-geolocation" spec="2.4.1" />
<plugin name="cordova-plugin-whitelist" spec="1.3.1" />
<access origin="*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<preference name="orientation" value="portrait" />
<!-- https://makeappicon.com/ios10icon -->
<icon src="res/icons/ios/Icon-App-20x20@2x.png" platform="ios" width="20" height="20" />
<icon src="res/icons/ios/Icon-App-20x20@3x.png" platform="ios" width="40" height="40" />
...
Only when I turn the app to the background, I receive the IOS request to allow the location request (should come when I first do the onclick -> geolocation while having the app in the foreground).
仅在应用程序后台激活听起来是 Content-Security-Policy
问题的症状 ()。
要解决问题,请确保您的 Content-Security-Policy
元标记包含 default-src
的 gap://ready
和 file:
条目。例如:
<meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src * 'unsafe-inline' 'unsafe-eval'">
Sometimes I get a geolocation result after a long time, sometimes not. I've tried all possible combinations on the three PositionOptions.
将 maximumAge
设置为 120000
意味着可以使用最多 2 分钟的位置(由 OS 缓存)。要强制新位置,请将其设置为零:
{
enableHighAccuracy: false
maximumAge: 0,
timeout: 2000
}
如果将 enableHighAccuracy
设置为 true,这会使用 GPS 硬件来锁定,因此请设置足够的超时以允许其锁定足够多的卫星:
{
enableHighAccuracy: true
maximumAge: 0,
timeout: 30000
}
有关 PositionOptions 的完整说明,请参阅 Mozilla docs
我是 PhoneGap Build 开发新手。在测试一些基本的应用程序功能时,我在 Iphone 6 / IOS 10 / PhoneGap Build 6.3.0.
上执行简单的地理定位请求时遇到超时问题重新安装应用程序后,我启动它并通过 onclick -> geolocation() 启动地理定位。
只有当我将应用程序转到后台时,我才会收到允许位置请求的 IOS 请求(应该在我第一次执行 onclick -> 地理定位时出现,同时将应用程序置于前台)。
有时我会在很长时间后得到地理定位结果,有时却得不到。我已经在三个 PositionOptions 上尝试了所有可能的组合。
当我询问 Google 地图应用程序时,它会立即显示位置。
知道我做错了什么吗?
谢谢你,金
function do_geolocation(){
alert('do geoloc');
navigator.geolocation.getCurrentPosition(geo_onSuccess, geo_onError, {maximumAge:120000, enableHighAccuracy:false} );
}
onclick=do_geolocation();
function geo_onSuccess(position){
alert('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');
var arr = new Array();
arr['lat'] = position.coords.latitude;
arr['lng'] = position.coords.longitude;
var x = new Date();var cb = x.getTime();
}
function geo_onError(position){
alert('code: '+error.code+'\nmessage: '+error.message+'\n');
return false;
}
<?xml version="1.0" encoding="UTF-8" ?>
<widget xmlns = "http://www.w3.org/ns/widgets"
xmlns:gap = "http://phonegap.com/ns/1.0"
id = "de.vvvvvv.secapp"
versionCode = "10"
version = "1.0.0" >
<!-- versionCode is optional and Android only -->
<name>vvvvvvv</name>
<description>
vvvvvvvvvvvv
</description>
<author href="http://vvvvvvv.de" email="info@vvvvvv.de">
Kim
</author>
<plugin name="cordova-plugin-geolocation" spec="2.4.1" />
<plugin name="cordova-plugin-whitelist" spec="1.3.1" />
<access origin="*"/>
<allow-intent href="http://*/*"/>
<allow-intent href="https://*/*"/>
<preference name="orientation" value="portrait" />
<!-- https://makeappicon.com/ios10icon -->
<icon src="res/icons/ios/Icon-App-20x20@2x.png" platform="ios" width="20" height="20" />
<icon src="res/icons/ios/Icon-App-20x20@3x.png" platform="ios" width="40" height="40" />
...
Only when I turn the app to the background, I receive the IOS request to allow the location request (should come when I first do the onclick -> geolocation while having the app in the foreground).
仅在应用程序后台激活听起来是 Content-Security-Policy
问题的症状 (
要解决问题,请确保您的 Content-Security-Policy
元标记包含 default-src
的 gap://ready
和 file:
条目。例如:
<meta http-equiv="Content-Security-Policy" content="default-src * gap://ready file:; style-src 'self' 'unsafe-inline'; img-src 'self' data:; script-src * 'unsafe-inline' 'unsafe-eval'">
Sometimes I get a geolocation result after a long time, sometimes not. I've tried all possible combinations on the three PositionOptions.
将 maximumAge
设置为 120000
意味着可以使用最多 2 分钟的位置(由 OS 缓存)。要强制新位置,请将其设置为零:
{
enableHighAccuracy: false
maximumAge: 0,
timeout: 2000
}
如果将 enableHighAccuracy
设置为 true,这会使用 GPS 硬件来锁定,因此请设置足够的超时以允许其锁定足够多的卫星:
{
enableHighAccuracy: true
maximumAge: 0,
timeout: 30000
}
有关 PositionOptions 的完整说明,请参阅 Mozilla docs