我正在运行 Mac OSX Lion 和 Xcode 4.1.1。我创建了一个新的 Phonegap 项目,并在 index.html 中添加了以下代码。
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js">
</script>
<!-- Tried setting sensor=true, didn't work too -->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false">
</script>
<script type="text/javascript" charset="utf-8" >
function display(position){
var initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
var myOptions = {
zoom: 12,
center: initialLocation,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}
function error()
{
alert('Not working');
}
function init()
{
navigator.geolocation.getCurrentPosition(display,error);
}
</script>
</head>
<body onload="init()">
<div id="map_canvas"></div>
</body>
</html>
然而,该代码拒绝工作。我什至在我的 Phonegap.plist 中启用了键“EnableLocation”。我没有收到任何警告说“Testapp 想知道您的位置,允许吗?”。我得到的只是控制台上的这个错误
navigator.geolocation.setError({ 'code': 1, 'message': 'Location Services Not Enabled' });
有人可以帮帮我吗?
最佳答案
我希望我知道该告诉你什么,但我希望这对你有所帮助。我认为你的代码很好。我刚刚在 Snow Leopard 上的 Dreamweaver 的 iOS 4.3 模拟器上运行它并且运行良好。但是,由于 DIV 完全折叠,我不得不进行一些调整。 map 在那里,但没有显示。
只是为了向自己证明这一点,尝试将其添加到您的 map_canvas div
style="height: 400px"
当我这样做时,我可以很好地看到 map 。
我不是 CSS 专家,所以我不知道如何让愚蠢的 div 扩展到 map 的大小。也许你必须用 CSS 来做。高度 100% 不起作用。有时候 CSS 真的很牛逼。
关于javascript - 在 iPhone 模拟器上显示谷歌地图 - Phonegap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7578031/