gpt4 book ai didi

javascript - 地理定位 Phonegap Android 不工作

转载 作者:行者123 更新时间:2023-11-28 09:47:05 24 4
gpt4 key购买 nike

我已尽一切努力让地理定位在我的模拟器(目标 2.2)上正常工作,我使用了 telnet 并发出了“地理修复”,然后是经度和纬度。我已经编辑了 Android list 以授予我权限,但我不确定为什么它不会向我显示以我的位置为中心的谷歌地图。如果我将任何坐标放入 map 的 setcentered 函数中,但是当我输入坐标时它不会显示。这是我已经检查了几个小时的代码:

<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=AIzaSyBrnId8UIeAeDTMr2sVi-g86izgdb2KhVo&sensor=true"
type="text/javascript"></script>
<script type="text/javascript">
// Wait for PhoneGap to load
// PhoneGap is ready
document.addEventListener("deviceready", onDeviceReady, false);
navigator.geolocation.getCurrentPosition(geolocationSuccess, [maximumAge: 3000, timeout: 5000, enableHighAccuracy:
<!--position.coords.latitude-->
initialize()
(GBrowserIsCompatible())
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(position.coords.latitude,position.coords.longitude), 13)
map.setUIToDefault();
// Add 10 markers to the map at random locations
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 500px; height: 300px"></div>
</body>
</html>

提前谢谢您,我已经尝试解决这个问题好几个小时了,我需要的是谷歌地图出现,而谷歌地图的中心就是我的位置。

最佳答案

以下是我的一些快速观察。您似乎没有定义在 PhoneGap 初始化时调用的 onDeviceReady 函数。下面是 PhoneGap 示例代码的副本,展示了如何使用 the PhoneGap documentation 中的 onDeviceReady 。

你看到这段代码是如何定义一个名为 onDeviceReady 的函数了吗?其中注释说 PhoneGap 方法可以安全调用。

您似乎也没有加载 PhoneGap JavaScript <script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script> .

<!DOCTYPE html>
<html>
<head>
<title>PhoneGap Device Ready Example</title>

<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Call onDeviceReady when PhoneGap is loaded.
//
// At this point, the document has loaded but phonegap-1.0.0.js has not.
// When PhoneGap is loaded and talking with the native device,
// it will call the event `deviceready`.
//
document.addEventListener("deviceready", onDeviceReady, false);

// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
// Now safe to use the PhoneGap API
}

</script>
</head>
<body>
</body>
</html>

此外,更多地查看您的代码,您没有使用 GBrowserIsCompatible()正确。您需要在 if 内使用此函数调用声明。

我将更改您的代码如下:

<script type="text/javascript" charset="utf-8" src="phonegap-1.0.0.js"></script>
<script type="text/javascript">
// Wait for PhoneGap to load
// PhoneGap is ready
document.addEventListener("deviceready", onDeviceReady, false);
<!--position.coords.latitude-->
function onDeviceReady() {
if (GBrowserIsCompatible()) {
navigator.geolocation.getCurrentPosition(geolocationSuccess, [maximumAge: 3000, timeout: 5000, enableHighAccuracy: // some code is missing here!
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(position.coords.latitude,position.coords.longitude), 13)
map.setUIToDefault();
// Add 10 markers to the map at random locations
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
} else {
// add code here to display error that device is not compatible
}
}
</script>
</head>
<body onunload="GUnload()">

关于javascript - 地理定位 Phonegap Android 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11651973/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com