javascript - 谷歌地图与 Phonegap 的集成

标签 javascript jquery google-maps cordova

我正在尝试在我的 Phonegap 应用程序中使用 Google Maps API v3。 目前我可以让 map 在我的本地主机上显示得很好,但是当我将其上传到 Phonegap 构建并尝试在 android 模拟器上查看它时,jquery 移动框架无法加载页面。

我很茫然,我已经查看了这里所有的 GMap 问题,但没有找到任何帮助。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=screen.width, initial-scale=1.0, user-scalable=no" />
    <title>DD Buddy App</title>
    <link rel="stylesheet" href="themes/ddBuddy.min.css" />
    <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile.structure-1.4.0.min.css" />
    <link rel="stylesheet" href="css/style.css" type="text/css" />
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script type="text/javascript">
    $(document).on('mobileinit', function () {
        $.mobile.defaultPageTransition = 'slide';
    });
    </script>
    <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
    <script src="js/parse-1.2.16.min.js"></script>
    <!-- cordova -->
    <script src="phonegap.js"></script>
</head>
<body class="site">
    <!-- Home/Landing Page -->
    <div data-role="page" data-theme="a" id="map">
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
        <script type="text/javascript" src="maps/jquery.ui.map.js"></script>
        <script type="text/javascript" src="maps/jquery.ui.map.services.js"></script>
        <div data-role="header" data-position="inline" class="header">
            <img src="img/logo.png" title="logo" class="logo" />
            <img src="img/home.png" title="home" class="homeImg" />
            <img src="img/menu.png" title="menu" class="menuImg" />
        </div>
        <div class="ui-content ui-page-theme-a" data-form="ui-page-theme-a" data-theme="a" role="main">
            <div id="map_canvas" style="width:100%;height:500px;background-color:#CCC;"></div>
        </div>
        <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true"></script>
        <script type="text/javascript" src="ui/jquery.ui.map.js"></script>
        <script type="text/javascript">
        $(function(){
            $("#map_canvas").gmap();
            $(".menuImg").on("touchend", function(){
                $.mobile.changePage("menu.html", {
                    transition: 'slide'
                });
            });
            $(".homeImg").on("touchend", function(){
                $.mobile.changePage("home.html", {
                    transition: 'slide'
                });
            });
        });
        </script>
    </div>
</body>
</html>

最佳答案

在继续之前(我将研究代码),我建议的第一件事是将所有脚本调用替换为本地脚本。我的意思是下载一个 jquery mobile 副本并将其存储在您的 Assets 文件夹中,而不是每次都从服务器调用它。所有的 CSS 和任何可以存储在本地的东西都是一样的。不要忘记您是在移动设备上进行部署,人们很可能会对他们的移动数据设置数据上限,因此您希望让您的应用程序尽可能精简数据。我会尽快更新答案,一旦我把一个版本放到我的手机上看看有什么问题。

埃米尔

你的代码很乱,看起来像是复制粘贴,甚至没有检查什么。我清理了一下它应该更接近于此:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=screen.width, initial-scale=1.0, user-scalable=no" />
    <title>DD Buddy App</title>
    <link rel="stylesheet" href="themes/ddBuddy.min.css" />
    <link rel="stylesheet" href="themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.0/jquery.mobile.structure-1.4.0.min.css" />
    <link rel="stylesheet" href="css/style.css" type="text/css" />
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.0/jquery.mobile-1.4.0.min.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
    <script type="text/javascript" src="ui/jquery.ui.map.js"></script>
    <script src="js/parse-1.2.16.min.js"></script>
    <script src="phonegap.js"></script>
    <!-- cordova -->
</head>
<body class="site">
    <!-- Home/Landing Page -->
    <div data-role="page" data-theme="a" id="map">
         <div data-role="header" data-position="inline" class="header">
            <img src="img/logo.png" title="logo" class="logo" />
            <img src="img/home.png" title="home" class="homeImg" />
            <img src="img/menu.png" title="menu" class="menuImg" />
        </div>
        <div class="ui-content ui-page-theme-a" data-form="ui-page-theme-a" data-theme="a" role="main">
            <div id="map_canvas" style="width:100%;height:500px;background-color:#CCC;"></div>
        </div>
        <script type="text/javascript">
        $(document).on('mobileinit', function () {
        $.mobile.defaultPageTransition = 'slide';
        });
            $(function(){
            $("#map_canvas").gmap();
            $(".menuImg").on("touchend", function(){
                $.mobile.changePage("menu.html", {
                    transition: 'slide'
                });
            });
            $(".homeImg").on("touchend", function(){
                $.mobile.changePage("home.html", {
                    transition: 'slide'
                });
            });
        });
        </script>
    </div>
</body>
</html>

注意事项: - 确保您的 asset/www 文件夹中有一个 UI 文件夹,其中包含 jquery.ui.map.js - 如前所述,下载副本并将每个 JS 和 CSS 的副本存储在本地,避免每次都调用互联网来获取它们 - 确保删除不必要的库(你在哪里使用解析 js?)

话虽这么说,如果您执行所有这些操作并使用上面的代码,您的手机上就会显示 map ,我不知道模拟器,因为我从不使用模拟器,我使用真实设备进行测试。

关于javascript - 谷歌地图与 Phonegap 的集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21864017/

相关文章:

javascript - 如何消除渐变中的间隙

javascript - 在 JQUERY 中延迟 | Ajax

java - 如何使用google map3在struts2中绘制折线

javascript - jQuery 插件,摇..

javascript - 使用 jQuery 从事件绑定(bind)调用代码

javascript - 如何在我的网站上添加 Google map ?

google-maps - Xamarin 谷歌地图自动完成

Javascript 代码以错误的顺序运行

javascript - 我如何独立使用 router.js?

javascript - 需要在 Bootstrap css 中单击时将图像加载为模态