基本信息和错误...
我正在尝试使用 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 列入白名单(尽管我觉得这可能是我的问题,请参见下图)。或者可能是我的代码(另见下文)。
所以基本上,有没有什么我明显遗漏或做错的事情?我已经对此进行了很长一段时间的研究,并且已经用完了阅读资源。任何帮助将不胜感激。
我的代码
白名单/外部主机?
我已经概述了我将 Google 域“列入白名单”的位置,但出于某种原因,我有点担心我是否正确地完成了这项工作。我经常阅读 where to find the ExternalHosts.plist文件,但在我创建 Phonegap 目录时没有该文件夹结构。因此,我对上面的 .plist 文件以及 config.xml 文件进行了编辑
代码
我的 index.html 页面非常基础。它包含通常的标题内容(CSS 文件包含、文档类型等)。 body 有
<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 “在 repo 协议(protocol)中)。无论哪种方式,您都会看到我的 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 map 显示在 Xcode 中的 iOS 模拟器上的任何帮助/提示,我们将不胜感激。
最佳答案
解决了!
在我的 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
作为协议(protocol)。否则,如果您通过 https
为应用程序提供服务,那么当然可以使用 https
。
希望这对将来的其他人有所帮助。所有这些工作都在我的 Github 上在我一直从事的名为 TrailFinder 的项目中。只需查看 Trailfinder/www 中的 Phonegap 内容即可。
关于javascript - Phonegap 无法读取/呈现 Google Maps API - "Can' t 查找变量 Google"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18578638/