【发布时间】:2023-02-16 04:43:10
【问题描述】:
尝试使用将在 Google MAP Javascript API 中跟踪用户位置的功能。此代码按预期工作,但它还会为每个 GPS 更新添加一个新标记。如何保留一个标记并让它更新到用户的位置?
function getLocation1(){
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.watchPosition(
(position) => {
const pos = {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
var lat = position.coords.latitude;
var lon = position.coords.longitude;
//const myLatLng = latlon ;
const locs = new google.maps.Marker({
position: { lat: lat, lng: lon },
map,
title: "",
});
if (locs == null) {
locs.setMap(map);
};
locs.setPosition(pos)
// infoWindow.setPosition(pos);
// infoWindow.setContent("You are here");
// infoWindow.open(map);
map.setCenter(pos);
map.setZoom(17);
},
() => {
handleLocationError(true, infoWindow, map.getCenter());
}
);
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
【问题讨论】:
标签: javascript gps maps