我有这组代码并且它有效,但每次我点击我的按钮获取我当前的位置时,它都会将我的地图定位到中心。是不是可以在不移动地图的情况下获取我的位置?
google.maps.event.addDomListener(controlText, 'click', function(self)
{
getLocation(self);
});
function getLocation(self)
{
console.log("in get location", self);
let locationOptions =
{
timeout : 60000,
enableHighAccuracy : true
};
navigator.geolocation.getCurrentPosition
(
(position) =>
{
self.curLat = position.coords.latitude;
self.curLong = position.coords.longitude;
console.log("updated position coord:"+self.curLat+","+self.curLong);
self.local = new Storage(LocalStorage);
self.local.set("currLong", self.curLong);
self.local.set("currLat", self.curLat);
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
markers = removeMarker(markers);
addMarker(map, markers);
},
(error) => {console.log(error);}, locationOptions
);
};
myLocationMarker = new google.maps.Marker
({
map : map,
icon : image,
animation : google.maps.Animation.DROP,
position : map.getCenter(),
id : 'myLocationMarker'
});
markers.push(myLocationMarker);
我通过更改
解决了我的问题map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
to
var latLng = new google.maps.LatLng(self.curLat, self.curLong);
并将数据传递给addMarker以将其用于位置。
答案 0 :(得分:0)
删除此行:
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));