简体   繁体   English

为什么PhoneGap Geolocation API导致我的页面加载如此缓慢?

[英]Why is PhoneGap Geolocation API causing my page to load so slowly?

Having a bit of an issue with using geolocation plugin in Cordova/PhoneGap 3.4 environment. 在Cordova / PhoneGap 3.4环境中使用地理位置插件存在一些问题。 I am not loading the Geolocation API in onDeviceReady as it relies on loading Google Maps API and I'm pretty sure any app submitted to App Store will be rejected if it requests from internet upon initial load (anyone feel free to confirm of disprove this). 我没有在onDeviceReady中加载Geolocation API,因为它依赖于加载Google Maps API,而且我很确定如果在初始加载时从互联网请求,则提交给App Store的任何应用都会被拒绝(任何人都可以确认以证明这一点) 。

I'm using Jquery Mobile 1.4.2 and placed the geolocation code onto a separate 'locator' page which does not load via ajax. 我正在使用Jquery Mobile 1.4.2,并将地理位置代码放置在单独的“定位器”页面上,该页面不会通过ajax加载。 Then I'm calling the geolocation on button to display user's location on Google Map. 然后,我在按钮上调用地理定位以在Google Map上显示用户的位置。

I'm testing out the app on an iPad and besides the fact that the Geolocation always seems to fail on detecting location the first time around (I've posted about this matter recently), the javascript seems to delay the page loading for up to almost 3 seconds (even on iOS simulator!). 我正在iPad上测试该应用程序,除了“地理位置”似乎始终无法在第一次检测到位置(我最近对此事发表过)的事实之外,JavaScript似乎最多延迟了页面加载时间将近3秒(甚至在iOS模拟器上也是如此!)。

I know excessive use of javascript can cause some lag in PG apps but I don't believe this is happening here. 我知道过多使用javascript可能会导致PG应用程序出现一些滞后,但我不认为这是在这里发生的。 I am calling all the javascript inline: 我正在调用所有的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>

I know it's definitely the Geolocation calls that are causing the lag as I removed them from the page and tested the page load time. 我知道肯定是Geolocation调用引起了延迟,因为我将它们从页面中删除并测试了页面加载时间。 Has anyone experienced the same sort of problem? 有没有人遇到过类似的问题? Does anyone have an idea why it affects the page load time so badly? 有谁知道为什么它如此严重地影响页面加载时间?

Bit of a slap in the face moment here. 在这里的脸上瞬间有点耳光。 Just realised it was the Google Map Api loaded in via javascript which causing the problem. 刚刚意识到这是通过javascript加载的Google Map Api引起了问题。 I've tried solving it by loading in the external script further down in the page but I've only experienced small improvement. 我尝试通过在页面的更下方加载外部脚本来解决该问题,但是我只经历了很小的改进。

The catch 22 is i need to call this JS in the page. 陷阱22是我需要在页面中调用此JS。 Is it possible to display a loader bar/graphic displays whilst page loads external js script so at least something is happening in the few seconds it downloads over relatively 'so-so' connections speed in Australia? 是否可以在页面加载外部js脚本时显示加载程序栏/图形显示,以便至少在几秒钟内通过相对“马马虎虎”的澳大利亚连接速度下载发生了某些事情?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM