【问题标题】:How to stop google map from shifting to center如何阻止谷歌地图转移到中心
【发布时间】:2016-07-12 08:34:22
【问题描述】:

我有这组代码并且它可以工作,但是每次我点击我的按钮来获取我的当前位置时,它都会将我的地图定位到中心。是否可以在不移动我的地图的同时获取我的位置?

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

【问题讨论】:

  • 您是否尝试过从传递给 google.maps.Marker 的对象中删除 position: map.getCenter() 属性?
  • 您好,我已经通过删除 map.setCenter 解决了这个问题。谢谢!

标签: google-maps google-maps-api-3


【解决方案1】:

删除这一行:

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

【讨论】:

    猜你喜欢
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    相关资源
    最近更新 更多