如何更改位置自定义控件位置谷歌地图?

时间:2018-03-22 08:01:51

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

我有自定义控件按钮,用于打开我的过滤器。当屏幕改变时,自定义控件按钮必须处于其他位置。

我写了那段代码

window.onresize = () => {
  if (screen.width <= 900) {
    this.map.controls[google.maps.ControlPosition.LEFT_TOP].pop($controlsTabs);
    this.map.controls[google.maps.ControlPosition.RIGHT_CENTER].push($controlsTabs);
  }
}

我收到错误

Uncaught TypeError: Cannot read property 'parentNode' of undefined

这是因为$controlsTabs没有LEFT_TOP位置。

如何编写正确的代码?

1 个答案:

答案 0 :(得分:0)

我决定了问题

window.onresize = () => {
  if (screen.width <= 900) {
    this.map.controls[google.maps.ControlPosition.LEFT_TOP].pop($controlsTabs);
    this.map.controls[google.maps.ControlPosition.RIGHT_CENTER].push($controlsTabs);
  } else {
    this.map.controls[google.maps.ControlPosition.RIGHT_CENTER].pop($controlsTabs); 
    this.map.controls[google.maps.ControlPosition.LEFT_TOP].push($controlsTabs);
  }

  google.maps.event.addDomListener(window, 'resize', () => {
    google.maps.event.trigger(this.map, 'resize');
  });
};