3

PhoneGapとjQueryMobileを使用してiPhoneアプリケーションを開発しています。私のアプリケーションには、2つのhtmlページがあります。1つはindex.htmlページで、2つ目はmapView.htmlです。現在、問題は、index.htmlからmapView.htmlを開いたときに発生します。

function loadMap() {
        $.mobile.changePage( "mapView.html", { transition: "pop"} );
}

マップが読み込まれていません。どのブラウザでもmapView.htmlを開くと
self.viewController.startPage = @"mapView.html";
、appDelegateクラスのマップロードを画面に変更してmapView.htmlを直接ロードしても、完全に機能します。別の.htmlページからマップを開いたときにマップがロードされない理由を誰かが知っていますか?私のPhoneGapバージョンは2.0.0
ありがとうございます。

編集1: mapView.htmlコード

<!DOCTYPE HTML>
<html>
    <head>
        <title>Map Location</title>
        <link rel="stylesheet" href="jquery.mobile-1.1.1.min.css" />
        <script type="text/javascript" src="jquery-1.7.1.min.js"></script>
        <script type="text/javascript" src="jquery.mobile-1.1.1.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"> </script>
        <script type="text/javascript">

            var demoCenter = new google.maps.LatLng(41,-87);

            var map;
            function initialize()
            {
                map = new google.maps.Map(document.getElementById('map_canvas'), {
                                          zoom: 7,
                                          center: demoCenter,
                                          mapTypeId: google.maps.MapTypeId.ROADMAP
                                          });
            }

            function addMarkers()
            {
                // perform your AJAX here. In this example the markers are loaded through the cityList array
                $.ajax({
                       type:'post',
                       url:'test.html',
                       data:'',
                       success:function(data)
                       {

                       // imagine that the data in this list
                       // will be retrieved from the AJAX response
                       // i used the cityList array just for testing
                       var cityList = [
                                       ['Chicago', 41.850033, -87.6500523, 1],
                                       ['Illinois', 40.797177,-89.406738, 2]
                                       ];

                       var marker, i;
                       var infowindow = new google.maps.InfoWindow();
                       for (i = 0; i < cityList.length; i++) 
                       {  
                       marker = new google.maps.Marker({
                                                       position: new google.maps.LatLng(cityList[i][1], cityList[i][2]),
                                                       map: map,
                                                       title: cityList[i][0]
                                                       });

                       google.maps.event.addListener(marker, 'click', (function(marker, i) {
                                                                       return function() {
                                                                       infowindow.setContent(cityList[i][0]);
                                                                       infowindow.open(map, marker);
                                                                       }
                                                                       })(marker, i));
                       }
                       }
                       });
            }

            $(document).ready(function() 
                              {
                              addMarkers();
                              });
            </script>
    </head>
    <body onload="initialize()">
        <div id="basic-map" data-role="page">
            <div data-role="header" data-theme="b">
                <h1>Map Location</h1>
                <a data-rel="back">Back</a>
            </div>
            <div data-role="content">   
                <div class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                    <div id="map_canvas" style="height:350px;"></div>
                </div>
            </div>
        </div>      
    </body>
</html>
4

1 に答える 1

2

$.mobile.changePagejQueryMobileAJAX機能を使用します。jQuery Mobileは、DOMの最初のdata-role="page"要素内にあるコードのみをロードします。

jQuery Mobileのドキュメントに記載されているように、 jQuery Mobileでは、ナビゲート時にAJAXを使用して各ページのコンテンツをDOMにロードし、DOMレディハンドラー$(document).ready()は最初のページに対してのみ実行されます。

以下に、2つのページを含む実際の例を示します。ナビゲーションは、Ajaxを介して実行され、マップは2番目のページ内にロードされます。

また、rel="external"またはdata-ajax="false"を使用して遷移を実行できることにも注意してください。これらの属性を使用すると、アニメーション化されたトランジションなしでページ全体が更新されます。

実例:

指示:

  • フォルダを作成する
  • フォルダ内にmaps.jsという名前のファイルを作成します
  • フォルダ内にmap-intro.htmlという名前のファイルを作成します
  • フォルダ内にmap.htmlという名前のファイルを作成します
  • 作成された各ファイルに、以下にある対応するコードを入力します

以下のコードをmaps.js内に追加します。

function initialize() {
    var mapCenter = new google.maps.LatLng(59.3426606750, 18.0736160278),
    myOptions = {
        zoom:10,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: mapCenter
    },
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}

$( document ).on( 'pageshow', '#map-page',function(event){
  initialize();
});

$( document ).on( 'click', '#map-anchor', function(event){
  event.preventDefault();
  $.mobile.changePage( "map.html", { transition: "flip" } );
});

map-intro.html内に以下のコードを追加します。

<!doctype html>
<html lang="en">
   <head>
        <title>Map Intro Page</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script>
        <script src="./maps.js"></script>
    </head>
    <body>
        <div id="map-intro-page" data-role="page">
            <div data-role="header">
                <h1><a data-ajax="false" href="/">Map Example</a></h1>
            </div>
            <div data-role="content">   
                <ul data-role="listview" id="my-list">
                    <li><a href="#" id="map-anchor">Go to Map</a></li>
                </ul>
            </div>
        </div>
    </body>
</html>

map.html内に以下のコードを追加します。

<!DOCTYPE html> 
<html> 
    <head> 
        <title>jQuery mobile with Google maps geo directions example</title>
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>
    </head>
    <body>
        <!-- /page -->
        <div data-role="page" id="map-page">
            <!-- /header -->
            <div data-role="header">
                <h1>Map</h1>
                <a data-rel="back">Back</a>
            </div>
            <!-- /content -->
            <div data-role="content" class="ui-bar-c ui-corner-all ui-shadow" style="padding:1em;">
                <div id="map_canvas" style="height:300px;"></div>
            </div>
        </div>
    </body>
</html>

これがお役に立てば幸いです。

于 2012-09-03T17:31:03.673 に答える