如何阻止谷歌地图转移到中心

时间:2016-07-12 08:34:23

标签: 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 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以将其用于位置。

1 个答案:

答案 0 :(得分:0)

删除此行:

map.setCenter(new google.maps.LatLng(self.curLat, self.curLong));