Phonegap无法读取/呈现Google Maps API - “无法找到变量Google”错误

时间:2013-09-02 18:01:56

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

基本信息和错误......

我正在尝试使用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网址列入白名单(虽然我觉得这可能是我的问题,见下图)。或者也许是我的代码(另见下文)。

所以基本上,是否有任何我遗漏或做错的事情显而易见?我已经研究了很长一段时间,并且已经耗尽了资源来阅读。任何帮助将不胜感激。

我的代码

白名单/ ExternalHosts?

whitelist issues? 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>

最后,我的 map.js 文件(以及您在上面看到的所有内容)都在我的Github仓库中(https://github.com/jamez14/TrailFinder/blob/master/TrailFinder/www/js/map.js) - 对于糟糕的命名约定(即调用我的Phonegap文件夹)感到抱歉回购中的“TrailFinder”)。无论哪种方式,您都会看到我的 map.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模拟器上,我们将非常感谢任何帮助/提示。

1 个答案:

答案 0 :(得分:8)

解决了!

在我包含Maps API和jQuery网址的 index.html 文件中,我有

<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内容即可。