【问题标题】:Phonegap unable to read/render Google Maps API - "Can't find variable Google" errorPhonegap 无法读取/呈现 Google Maps API - “找不到变量 Google”错误
【发布时间】:2013-09-05 21:11:02
【问题描述】:

基本信息和错误...

我正在尝试使用 Google Maps API v3、Phonegap 2.9.0 和 Xcode 4.6.3 创建一个应用程序,该应用程序可以获取用户的当前位置,然后向他们返回一组最近的位置列表(使用 Fusion表)。这一切都可以在我桌面上的任何浏览器中运行,但是一旦“移植”到 Phonegap,我就会收到一个错误。

我收到的错误是:

ReferenceError: Can't find variable: google in (/somepath/)

当我在 Xcode 中的 iPhone 模拟器中运行应用程序时。

我尝试过的

我已经阅读了无数关于此问题的文档,并尝试了几乎所有推荐的修复方法。我试图将我调用的 API url 列入白名单(尽管我觉得这可能是我的问题,见下图)。或者可能是我的代码(另见下文)。

所以基本上,我是否遗漏了什么或做错了什么是显而易见的?我已经研究了很长时间,并且已经没有资源可供阅读。任何帮助将不胜感激。

我的代码

白名单/外部主机?

bigger image

我已经概述了我已将 Google 域“列入白名单”的位置,但出于某种原因,我对我是否正确执行此操作有点谨慎。我经常阅读where to find the ExternalHosts.plist 文件,但在创建Phonegap 目录时没有该文件夹结构。因此,我对上面的 .plist 文件以及 config.xml 文件

进行了编辑

代码

我的 index.html 页面非常基础。它包含通常的标题内容(CSS 文件包括、doctype 等)。身体有

<div id="map" class="google-map-canvas"></div>

<table class="table table-condensed table-striped">
    <thead>
        <tr>
            <th>Name</th>
            <th>Location</th>
            <th>Distance</th>
        </tr>
    </thead>
    <tbody id="sidebar-data">
                <!--data gets put into here-->
    </tbody>
</table>

页脚有这个...

<script src="//maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="cordova.js"></script>
<script src="js/map.js"></script>

最后,我的 ma​​p.js 文件(以及您在上面看到的所有其他内容)都在我的 Github 存储库 (https://github.com/jamez14/TrailFinder/blob/master/TrailFinder/www/js/map.js) 中 - 抱歉命名约定不佳(即调用我的 Phonegap 文件夹“TrailFinder” “在回购协议中)。无论哪种方式,您都会看到我的 ma​​p.js 文件包含通常的“Phonegap 地理定位内容”。

我也尝试了这个关于添加 Maps API 调用 (Google Maps API v3 not working in mobile browsers or PhoneGap) 的建议,但没有成功。

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    alert('onDeviceReady');
    navigator.geolocation.getCurrentPosition(onSuccess, onError);
}

window.onerror = function(message, url, lineNumber) {
    alert("Error: "+message+" in "+url+" at line "+lineNumber);
}

var map;

function onSuccess(position) {
//lots of stuff below this in the actual file

再次感谢任何有关如何让我的 Google 地图显示在 Xcode 中的 iOS 模拟器上的帮助/提示。

【问题讨论】:

    标签: javascript ios google-maps-api-3 cordova geolocation


    【解决方案1】:

    解决了!

    在我的 index.html 文件中,当我包含 Maps API 和 jQuery URL 时,我有

    <script src="//maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
    

    当我应该放...

    <script src="http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
    

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    

    如果您只是在本地运行应用程序,而不是通过https,则使用http 作为协议。否则,如果您通过https 为应用程序提供服务,那么当然可以使用https

    希望这对将来的其他人有所帮助。所有这些工作都由我的Github 在我一直在从事的一个名为 TrailFinder 的项目中完成。只需查看 Trailfinder/www 了解 Phonegap 的内容。

    【讨论】:

    • 哇!我从没想过这会是个问题。
    • 刚刚在 Cordova 项目中遇到了同样的事情。仅在我的开发浏览器中使用 // 即可正常工作,但在设备上出现“找不到变量 google”错误而失败。谢谢 jamez14!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-08
    相关资源
    最近更新 更多