[英]getCurrentPosition phonegap (cordova 1.6) onSuccess fires never (but iphone device&sim work perfectly)
[英]Why does PhoneGap Geolocation onSuccess never fires the first time around?
今晚我一直在搜索stackoverflow几个小时,以寻找解决此问题的方法,我强烈认为这是Geolocation的cordova / phonegap错误。
我在geoLocation遇到与@Tal相同的问题,是否启用了页面刷新功能?
基本上,getLocation调用中的onSuccess永远不会在您第一次请求位置时被调用。 我已经设置了一个超时时间来调用onError并让页面休息,但是仍然无法弄清为什么第一次无法正常工作,只有完全刷新才能解决问题。 将其放入循环,即:
function onError(error) {
if (error.code === PositionError.TIMEOUT){
navigator.geolocation.getCurrentPosition(onSuccess, onError,
{ maximumAge: 3000, timeout: 1000, enableHighAccuracy: true } );
}
// Handle other errors here
}
似乎也无法正常工作。 我试过重新安排我的代码顺序和功能,但这似乎没有任何区别。 该应用程序将在第一次失败后才成功获取GPS坐标。 我真的认为这是科尔多瓦虫。 我的完整JavaScript代码如下:
<script type="text/javascript" charset="utf-8">
// onSuccess Geolocation
//
function onSuccess(position) {
var element = document.getElementById('geolocation');
//OUTPUT COORDINATES INFORMATION INTO HTML
element.innerHTML = '<h2>Detailed Gelocation information</h2><table class="geoTable"><tr><td class="noborder"></td><th>Latitude</th><th>Longitude</th><th>Altitude</th><th>Accuracy</th><th>Altitude Accuracy</th><th>Heading</th><th>Speed</th><th>Timestamp</th></tr>' +
'<tr><td class="head">Data Value</td>' +
'<td class="centre">' + position.coords.latitude + '</td>' +
'<td class="centre">' + position.coords.longitude + '</td>' +
'<td class="centre">' + position.coords.altitude + '</td>' +
'<td class="centre">' + position.coords.accuracy + '</td>' +
'<td class="centre">' + position.coords.altitudeAccuracy + '</td>' +
'<td class="centre">' + position.coords.heading + '</td>' +
'<td class="centre">' + position.coords.speed + '</td>' +
'<td class="centre">' + new Date(position.timestamp) + '</td></tr>'+
'</table>' +
'<p align="center"><button data-theme="h" onclick="resetLocation();">Clear GPS Location</button></p>';
//GET LOCATION VARIABLES FROM API
var lat = position.coords.latitude;
var lng = position.coords.longitude;
var yourStartLatLng = new google.maps.LatLng(lat, lng);
//PUT GMAPS INFORMATION INTO PAGE
$('#map_canvas').gmap({'center': yourStartLatLng});
$('#map_canvas').gmap('option', 'zoom', 19);
$('#map_canvas').gmap('option', 'mapTypeId', google.maps.MapTypeId.SATELLITE);
$('#map_canvas').gmap('addMarker', {
'position': yourStartLatLng,
'draggable': false,
'bounds': false
});
}
// onError Callback sends user an alert message and refreshes screen to load all scripts again
//
function onError(error) {
alert('The Freshwater application failed to get your location. Please ensure that you have a successful WIFI or 3G/4G internet connection when using the Geolocation feature. ' +
'The Locator screen will now be refreshed. Please attempt Geolocation again.');
//navigator.geolocation.getCurrentPosition(onSuccess, onError,
//{ maximumAge: 3000, timeout: 5000, enableHighAccuracy: false } );
//PUT A TIMER ON ERROR TO REFRESH PAGE FOR GPS
setTimeout("window.location+='?refreshed';", .1000);
}
//RESET GEOLOCATION PAGE TO TAKE ANOTHER LOCATION READING
function resetLocation(){
setTimeout("window.location+='?refreshed';", .1000);
}
//ATTACH FUNCTION TO BUTTON TO CALL GEOLOCATION
function getLocation() {
navigator.geolocation.getCurrentPosition(onSuccess ,onError,
{ timeout: 1000, enableHighAccuracy: true } );
}
</script>
是Javascript的加载时间导致地理定位功能首次超时。
我对脚本进行了一些修改,并摆脱了一些我没有使用过的Gmap UI Jquery库。
jquery.ui.map.extensions.js
jquery.ui.map.microdata.js
jquery.ui.map.microformat.js
jquery.ui.map.overlays.js
jquery.ui.map.rdfa.js
jquery.ui.map.services.js
这可能影响很小。 当使用iOS模拟器进行一些基于时间的测试时,页面加载后立即要求进行地理位置定位永远不会在onSuccess上首次触发。 如果我在等待10秒钟后才单击“获取位置”按钮,则它会在第一次运行。
显然,这与在请求地理位置之前正确加载所有(内部和外部)脚本有关。 我已经预加载了外部google map脚本,如下所示:
function onLoad() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?key=AIzaSyDy1bBAiDhmFfuKz6SerLvKyOIUqlFsX7c&sensor=true&' +
'callback=mapLoaded';
document.body.appendChild(script);
}
但是,如果有人请求地理定位的速度过快,则似乎超时并在第一次运行onError。
更新:我决定隐藏请求地理位置的按钮,并在加载外部脚本后显示它(并放置一个加载栏gif)。 请求位置时仍然会出错。 最有可能的原因是Google api代码本身包含一个getScript命令(所以我没有看到真正的加载时间)。
也许是受Google摆布?
我认为这可能是因为您的设备未就绪。 请参见以下代码:
<script>
function onDeviceReady() {
// Do your geolocation call here ...
}
document.addEventListener("deviceready", onDeviceReady, false);
</script>
如果您将地理位置呼叫置于设备就绪事件中,则只有在您的设备被视为“就绪”后,它们才会触发。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.