我正在尝试重新设计 phonegap 应用程序以使用 googlemaps 插件和 v2 的 google maps API。
我正在努力更新标记位置,这似乎在 v3 中完美运行,但不适用于插件。
问题似乎是在调用时setMarkerPosition()
,标记对象没有被传递给它,所以我得到“无法调用未定义的方法 setPosition”
我已currentPositionMarker
在脚本顶部设置为全局变量,虽然我setCurrentPosition()
在一开始就定义了它,但我也尝试使用回调再次定义它,但都不起作用。
抱歉,如果它有些愚蠢,这是我的第一个 JavaScript 项目,因此对语言的某些部分的理解仍然非常不完整,因此非常感谢任何指针。
我正在尝试的代码...
function initializeMap()
{ map = plugin.google.maps.Map.getMap();
// map = new plugin.google.maps.Map(document.getElementById('map_canvas'), {
// zoom: 13,
// });
}
function initLocationProcedure() {
initializeMap();
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
displayAndWatch,
errorCallback_highAccuracy,
{maximumAge:600000, timeout:5000, enableHighAccuracy: true});
} else {
alert("Your Phone does not support Geolocation");
}
}
function displayAndWatch(position) {
// set current position
setCurrentPosition(position);
// watch position
watchCurrentPosition();
}
function errorCallback_highAccuracy(position) {
}
function watchCurrentPosition() {
var positionTimer = navigator.geolocation.watchPosition(
function (position) { setMarkerPosition(currentPositionMarker,position);
}, error, {maximumAge:600000, timeout:5000, enableHighAccuracy: true});
}
function error(){
}
function setMarkerPosition(marker, position) {
marker.setPosition(
new plugin.google.maps.LatLng(
position.coords.latitude,
position.coords.longitude)
);
}
function setCurrentPosition(pos) {
currentPositionMarker = map.addMarker({
'position': new plugin.google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
),
}, function(marker) {
currentPositionMarker = marker;
});
map.setCenter(new plugin.google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
}