Google maps 如何阻止谷歌地图向中心转移
我有这组代码,它可以工作,但是每次我点击我的按钮获取我的当前位置,它都会将我的地图定位到中心。是否可以在不移动地图的情况下获取我的位置Google maps 如何阻止谷歌地图向中心转移,google-maps,google-maps-api-3,Google Maps,Google Maps Api 3,我有这组代码,它可以工作,但是每次我点击我的按钮获取我的当前位置,它都会将我的地图定位到中心。是否可以在不移动地图的情况下获取我的位置 google.maps.event.addDomListener(controlText, 'click', function(self) { getLocation(self); }); function getLocation(self) { console.log("in get location", self); let lo
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);
我已经通过改变来解决我的问题
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
to
var latLng = new google.maps.LatLng(self.curLat, self.curLong);
并将数据传递给addMarker以将其用于位置。删除此行:
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
to
var latLng = new google.maps.LatLng(self.curLat, self.curLong);
map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));
您是否尝试过从传递给google.maps.Marker的对象中删除
position:map.getCenter()
属性?您好,我已通过删除map.setCenter解决了此问题。谢谢