【问题标题】:Allow user to add marker to google maps only at specific zoom level允许用户仅在特定缩放级别向谷歌地图添加标记
【发布时间】:2018-09-18 18:48:52
【问题描述】:

我有一个网站,允许用户使用标记/图钉在谷歌地图上添加他们的属性,我想(强制)用户进入最大允许缩放级别,以便能够设置图钉/标记,以便拥有该物业的具体确切位置。

【问题讨论】:

  • 问题是???
  • 如何仅在特定缩放时显示标记,并在他/她点击任何更大的缩放时显示消息(请放大到属性位置)?
  • 您现有的代码是什么样的?请提供一个 minimal reproducible example 来证明您的问题。

标签: javascript google-maps google-maps-markers


【解决方案1】:

您可以在Map 对象上使用getZoom() 方法,并在允许创建新的Marker 之前检查当前的缩放级别。

这是一个 simple sample JSBin 改编自 Google 文档中的 Remove Markers sample。相关代码:

// Adds a marker to the map and push to the array.
function addMarker(location) {
  if (map.getZoom() < 17) {
    alert('please zoom in closer to add a new marker');
  } else {
    var marker = new google.maps.Marker({
        position: location,
        map: map
    });
    markers.push(marker);
  }
}

【讨论】:

  • 为什么是17?这是最大缩放级别吗?无处不在?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-14
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
相关资源
最近更新 更多