地图视图未使用PhoneGap加载 [英] Map view is not loading with PhoneGap

查看:113
本文介绍了地图视图未使用PhoneGap加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用PhoneGap和jQueryMobile开发一个iPhone应用程序,在我的应用程序我有两个html页面,首先是index.html页面,第二是mapView.html,现在,我的问题是当我从索引打开mapView.html。 html使用

I am developing an iPhone application using PhoneGap and jQueryMobile, in my application I have two html pages, first is index.html page and second is mapView.html, now, my problem is when I open mapView.html from index.html using

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

地图未加载。如果我在任何浏览器上打开mapView.html,它都能正常工作,即使我通过更改

自动加载mapView.html self.viewController.startPage = @mapView.html;

在appDelegate类映射加载到屏幕上。任何人都有什么想法为什么地图没有加载,当我打开它从另一个.html页面?我的PhoneGap版本是2.0.0

谢谢。

map is not loading. If I open mapView.html on any browser it works perfectly, even if I load mapView.html directly by changing
self.viewController.startPage = @"mapView.html";
in appDelegate class map loads on screen. Do anyone have any idea why map is not loading when I open it from another .html page?My PhoneGap version is 2.0.0
Thanks.

编辑1: mapView.html代码

EDIT 1:mapView.html code

<!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>


推荐答案

$。 changePage 使用jQuery Mobile AJAX功能。 jQuery Mobile只加载DOM中第一个data-role =page元素内的代码。

The $.mobile.changePage uses the jQuery Mobile AJAX functionality. jQuery Mobile loads only the code which is inside the first data-role="page" element in the DOM.


a href =http://jquerymobile.com/demos//1.2.0/docs/api/events.html =nofollow> jQuery Mobile docs ,在jQuery Mobile中,AJAX用于加载将每个页面的内容导入到您的DOM中,并且DOM准备处理程序 $(document).ready()仅对第一页执行。

As stated in the jQuery Mobile docs , in jQuery Mobile, AJAX is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler $(document).ready() only executes for the first page.

下面你可以找到一个工作示例,其中包括两个页面,导航是通过Ajax执行的,地图加载到第二个页面中:

Below you can find a working example which includes two pages, the navigation is performed through Ajax and the map is loaded inside the second page:

另请注意,您可以使用rel =external或data-ajax =false执行转换。这些属性的使用将导致整个页面的刷新而不进行动画转换。

Also note that you can perform a transition using rel="external" or data-ajax="false". The usage of these attributes will cause a full page refresh without animated transition.

工作示例:


  • 创建文件夹

  • 在文件夹中创建名称为maps.js的文件夹

  • 在文件夹中创建名为map-intro.html的文件

  • 在文件夹中创建名为map.html的文件

  • 在每个创建的文件中填入以下可以找到的相应代码

  • Create a folder
  • Create a file with name maps.js inside the folder
  • Create a file with name map-intro.html inside the folder
  • Create a file with name map.html inside the folder
  • Fill each one of the created files with the corresponding code which can be found below

在地图中添加以下代码.js:

Add the below code inside the 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中添加以下代码:

Add the below code inside the 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中添加以下代码:

Add the below code inside the 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>

我希望这有助于。

这篇关于地图视图未使用PhoneGap加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆