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

jam*_*mes 4 javascript geolocation google-maps-api-3 ios cordova

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

我正在尝试使用Google Maps API v3,Phonegap 2.9.0和Xcode 4.6.3来创建一个获取用户当前位置的应用,然后向他们返回一组最近位置的列表(使用Fusion Tables).这一切都在我的桌面上的任何浏览器中工作,但一旦"移植"到Phonegap,我收到一个错误.

我收到的错误是:

ReferenceError: Can't find variable: google in (/somepath/)
Run Code Online (Sandbox Code Playgroud)

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

我试过了什么

我已经阅读了关于这个问题的无数文档,并尝试了大多数建议的修复程序.我试过将我调用的API网址列入白名单(虽然我觉得这可能是我的问题,见下图).或者也许是我的代码(另见下文).

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

我的代码

白名单/ ExternalHosts?

白名单问题? 更大的形象

我概述了我将Google域名"列入白名单"的地方,但出于某种原因,我对是否正确完成此操作有点担心.我经常阅读在哪里可以找到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>
Run Code Online (Sandbox Code Playgroud)

页脚有这个......

<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>
Run Code Online (Sandbox Code Playgroud)

最后,我的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无法在移动浏览器或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
Run Code Online (Sandbox Code Playgroud)

再一次,任何帮助/提示将非常感谢如何让我的谷歌地图显示在Xcode的iOS模拟器上.

jam*_*mes 8

解决了!

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

<script src="//maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
Run Code Online (Sandbox Code Playgroud)

什么时候我应该...

<script src="http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
Run Code Online (Sandbox Code Playgroud)

和

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

如果您只是在本地运行应用程序而不是在本地运行https,那么http请将其用作协议.否则,如果您正在为该应用程序提供服务,https那么请使用https相反的方法.

希望这有助于将来的其他人.所有这些工作都在我的Github上,我正在研究一个名为TrailFinder的项目.只需查看Trailfinder/www的Phonegap内容即可.

  • 刚刚在Cordova项目中遇到了同样的事情.只是在我的开发浏览器中工作得很好,但在设备上找不到"无法找到变量谷歌"错误.谢谢jamez14! (2认同)