在我刷新之前,jQuery Mobile页面不显示内容

时间:2015-08-31 14:40:07

标签: jquery-mobile page-refresh

为什么我的html页面包含两个' Mobile'里面的页面必须刷新或移动小边缘以显示我的地图吗?请参阅my fiddle或以下代码:

我有一个移动页面文档(html),它有两个jQuery Mobile页面。 1.登陆页面,要求您知道您的位置。 2. OpenLayers3地图页面,它在您打开时占据您的位置并将地图置于其中心。

我的麻烦在于:地图将会建立并且它将以位置为中心但直到我调整大小后才会渲染地图'边缘或浏览器窗口。我怀疑它与页面事件有关,但我不确定。

是否有遗失的财产?

安迪

   <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>MobilePg</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css"/>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>    
    <link rel="stylesheet" href="http://openlayers.org/en/v3.0.0/css/ol.css"/>
    <script type="text/javascript" src=" http://openlayers.org/en/v3.0.0/build/ol.js"></script>

    <style>
        #myFooterPosit {
            color: gray;
        }
    </style>

    <script>
        var x, y;
        $(document).ready(function () {
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(showPosition);
            }           
        });

        function showPosition(position) {
            var positThing = $('#myFooterPosit');

            positThing.text('lat: ' + position.coords.latitude + " : " + " long: " + position.coords.longitude);

            x = position.coords.latitude;
            y = position.coords.longitude;

        }


    </script>

</head>
<body>
    <script>
        $(document).on("pagebeforeshow", "#mapPage", function () {
            makeMap();
        })
    </script>
    <!-- Landing page Point of Entry-->
    <div data-role="page" id="homePage">
        <div data-role="header">
            <a href="#mapPage" class="ui-btn ui-corner-all ui-shadow ui-icon-location ui-btn-icon-left">Map</a>

            <h1>Mbl Map Input</h1>
            <a href="#" class="ui-btn ui-corner-all ui-shadow ui-icon-search ui-btn-icon-left">Search</a>

        </div>
        <div data-role="main" class="ui-content">
            <p>My Content..</p>
        </div>
        <div data-role="footer">
            <h1><span id="myFooterPosit"></span></h1>

        </div>
    </div>


    <!-- Map Page -->
    <script>
        function makeMap() {
            try {
                alert(x + " : " + y);
                var map = new ol.Map({
                    target: 'map',
                    layers: [
                    new ol.layer.Tile({
                        title: 'OSM',
                        type: 'base',
                        visible: true,
                        source: new ol.source.OSM()
                    })],
                    view: new ol.View({
                        center: ol.proj.transform([y, x], 'EPSG:4326', 'EPSG:3857'),
                        zoom: 14
                    })
                });
            } catch (e) {
                alert(e.message);
            }
        }
    </script>
    <div data-role="page" id="mapPage">
        <div data-role="header">
            <a href="#homePage" class="ui-btn ui-corner-all ui-shadow ui-icon-home ui-btn-icon-left">Home</a>

            <h1>Map</h1>
        </div>
        <div data-role="main" class="ui-content">
            <div id="map" class="map" style="height:200px;"></div>
        </div>


        <div data-role="footer">
            <h1><span id="myFooterPosit"></h1>

        </div>
    </div>
</body>
</html

&GT;

1 个答案:

答案 0 :(得分:0)

您可以使用pagecontainer小部件的show event:

http://api.jquerymobile.com/pagecontainer/#event-show

  

更新了 FIDDLE

var x, y;

$(document).on("pagecreate","#homePage", function(){ 
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    }
});
$(document).on( "pagecontainershow", function( event, ui ) {
    if (ui.toPage.prop("id") == "mapPage"){
        makeMap();
    }
});

function showPosition(position) {
    var positThing = $('#myFooterPosit');
    positThing.text('lat: ' + position.coords.latitude + " : " + " long: " + position.coords.longitude);
    x = position.coords.latitude;
    y = position.coords.longitude;   
}

function makeMap() {
    try {
        var map = new ol.Map({
            target: 'map',
            layers: [
            new ol.layer.Tile({
                title: 'OSM',
                type: 'base',
                visible: true,
                source: new ol.source.OSM()
            })],
            view: new ol.View({
                center: ol.proj.transform([y, x], 'EPSG:4326', 'EPSG:3857'),
                zoom: 14
            })
        });
    } catch (e) {
        alert(e.message);
    }
}