使用 PhoneGap 和 googlemaps 插件更新位置跟踪的标记位置
Updating Marker Position for Location Tracking with PhoneGap and googlemaps plugin
我正在尝试重新设计一个 phonegap 应用程序以使用 googlemaps 插件和 google 地图的 v2 API。
我正在努力更新标记位置,这似乎在 v3 中完美运行,但不适用于插件。
问题似乎是在调用 setMarkerPosition()
时,没有将标记对象传递给它,所以我得到 "cannot call method setPosition of undefined"
我在我的脚本顶部将 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
));
}
好的,事实证明正在创建标记变量,但没有像 setCurrentPosition()
那样及时,这正在设置标记正在与 setMarkerPosition()
异步调用。完全删除 displayAndWatch()
然后从 setCurrentPosition()
的回调中调用 setMarkerPosition()
似乎是解决方法。
function setCurrentPosition(pos) {
map.addMarker({
'position': new plugin.google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
),
}, function(marker) {
currentPositionMarker = marker;
watchCurrentPosition();
});
map.setCenter(new plugin.google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
}
我正在尝试重新设计一个 phonegap 应用程序以使用 googlemaps 插件和 google 地图的 v2 API。
我正在努力更新标记位置,这似乎在 v3 中完美运行,但不适用于插件。
问题似乎是在调用 setMarkerPosition()
时,没有将标记对象传递给它,所以我得到 "cannot call method setPosition of undefined"
我在我的脚本顶部将 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
));
}
好的,事实证明正在创建标记变量,但没有像 setCurrentPosition()
那样及时,这正在设置标记正在与 setMarkerPosition()
异步调用。完全删除 displayAndWatch()
然后从 setCurrentPosition()
的回调中调用 setMarkerPosition()
似乎是解决方法。
function setCurrentPosition(pos) {
map.addMarker({
'position': new plugin.google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
),
}, function(marker) {
currentPositionMarker = marker;
watchCurrentPosition();
});
map.setCenter(new plugin.google.maps.LatLng(
pos.coords.latitude,
pos.coords.longitude
));
}