[英]Why is PhoneGap Geolocation API causing my page to load so slowly?
在Cordova / PhoneGap 3.4環境中使用地理位置插件存在一些問題。 我沒有在onDeviceReady中加載Geolocation API,因為它依賴於加載Google Maps API,而且我很確定如果在初始加載時從互聯網請求,則提交給App Store的任何應用都會被拒絕(任何人都可以確認以證明這一點) 。
我正在使用Jquery Mobile 1.4.2,並將地理位置代碼放置在單獨的“定位器”頁面上,該頁面不會通過ajax加載。 然后,我在按鈕上調用地理定位以在Google Map上顯示用戶的位置。
我正在iPad上測試該應用程序,除了“地理位置”似乎始終無法在第一次檢測到位置(我最近對此事發表過)的事實之外,JavaScript似乎最多延遲了頁面加載時間將近3秒(甚至在iOS模擬器上也是如此!)。
我知道過多使用javascript可能會導致PG應用程序出現一些滯后,但我不認為這是在這里發生的。 我正在調用所有的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><br /></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>
我知道肯定是Geolocation調用引起了延遲,因為我將它們從頁面中刪除並測試了頁面加載時間。 有沒有人遇到過類似的問題? 有誰知道為什么它如此嚴重地影響頁面加載時間?
在這里的臉上瞬間有點耳光。 剛剛意識到這是通過javascript加載的Google Map Api引起了問題。 我嘗試通過在頁面的更下方加載外部腳本來解決該問題,但是我只經歷了很小的改進。
陷阱22是我需要在頁面中調用此JS。 是否可以在頁面加載外部js腳本時顯示加載程序欄/圖形顯示,以便至少在幾秒鍾內通過相對“馬馬虎虎”的澳大利亞連接速度下載發生了某些事情?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.