【问题标题】:Set initial center and zoom in google maps API设置初始中心并放大 google maps API
【发布时间】:2020-02-22 11:00:40
【问题描述】:

如果我们直接打开google map,初始视图是我们的城市,这个城市被放大并适合整个屏幕。

如果我们基于google maps API创建一个map web app(不设置center和zoom参数),初始视图是空白的。

问题是:如何使地图网络应用程序的行为与谷歌地图相同(例如,将初始地图视图显示为用户所在的城市被全屏显示)?

期待答案,并期望尽可能少的代码。

对于 Bing Maps API,城市在不设置地图中心和缩放比例的情况下适合视图。

【问题讨论】:

  • 您是指在地图初始化时定位用户吗?就像在这个例子中一样? developers.google.com/maps/documentation/javascript/geolocation
  • 不完全是。我希望我的地图网络应用程序(使用谷歌地图 api)的初始视图与谷歌地图的行为相同。这意味着,如果来自纽约的用户打开应用程序,初始视图是纽约市,并且城市的边界被缩放并适合屏幕,洛杉矶用户 LA 被适当地缩放并占据整个屏幕。
  • 由于只有用户所在的城市是相关的,所以最好使用融合的位置API(仅当它存在时)。如果使用 Geolocation 需要用户批准。
  • 融合位置 API 适用于移动 AFAIK。我也看不出为什么 HTML5 Geolocation 不适合您的用例?你总是可以增加初始缩放,使用城市边界,设置完整的地图高度+宽度......我建议你通过 JS API 的指南和代码示例developers-dot-devsite-v2-prod.appspot.com/maps/documentation/…developers-dot-devsite-v2-prod.appspot.com/maps/documentation/… 并自己确定最适合你的。
  • 期待答案,并期望尽可能少的代码。 - 在提出问题时,我们希望提供以下代码您正在使用并且遇到问题。这个问题对于本网站来说是题外话。在网络上进行简单的搜索将在几秒钟内回答这个问题。

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


【解决方案1】:

您提到您希望地图最初设置在用户所在的城市上。基于这个用例,我们有必要首先知道我们的用户在哪个城市。一种检测方法是使用 HTML5 的 Geolocation API。

我知道您发现请求权限的弹出窗口很烦人,但请注意,如 Geolocation API 的文档 (Item 4.1) 中所述

未经用户明确许可,用户代理不得向网站发送位置信息。

这就是为什么要求弹出窗口要求权限的原因。

假设你对它没问题(如果你愿意,你可以随时选择使用其他用户位置检测策略),然后,在获取目标用户的位置后(以地理坐标的形式) ,您可以使用Geocoding API 对其进行反向地理编码。这将为我们提供与这些坐标匹配的地址列表。

从此地址列表中,您可以选择与其所在城市相对应的地址。请注意,对于这部分,您可能需要使用不同的组件集来与不同地区使用的地址格式保持一致。

根据这个doc,类型为locality 的地方表示一个合并的城市或城镇政治实体。

但是,在某些情况下,例如在英国和瑞典,显示城市的组件是 postal_town。布鲁克林是另一个特例,因为它使用sublocality_level_1 来代表这座城市。我在下面的代码示例中考虑了这些特殊情况。

选择城市地址后,您可以获得其locationviewport 并在您将实例化的地图中使用它们。

但请根据您的用例随意修改以下代码示例:

function initmap() {
// Get location first using HTML5 Geolocation API
  if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(geocodePosition);
  } else {
    alert("Geolocation is not supported by this browser.");
  }
}

function geocodePosition(position) {
  var latlng = {
    lat: position.coords.latitude,
    lng: position.coords.longitude
  };

  // GB is the country code for United Kingdom of Great Britain and Northern Island
  // SE is for Sweden
  var specialCaseCountries = ['GB', 'SE'];
  var specialCaseState = ['Brooklyn'];
  var geocoder = new google.maps.Geocoder();
  var map;

  // reverse geocode the geographical coordinates from the Geolocation API
  geocoder.geocode({
    'location': latlng
  }, function(results, status) {
    // variable for the selected address that corresponds as the city
    var city;
    if (status === 'OK') {
      results.map(function(k, i) {
        results[i].address_components.map(function(a, b) {
          // checking if the result is included on the specialCaseCountries
          if (specialCaseCountries.includes(a.short_name)) {
            if (results[i].types.includes('postal_town')) {
              console.log('formatted address: ', results[i].formatted_address);
              city = results[i];
            }
            // checking if the result is included on the specialCaseState
          } else if (specialCaseState.includes(a.short_name)) {
            if (results[i].types.includes('sublocality_level_1')) {
              console.log('formatted address: ', results[i].formatted_address);
              city = results[i];
            }
            // other addresses
          } else {
            if (results[i].types.includes('locality')) {
              console.log('formatted address: ', results[i].formatted_address);
              city = results[i];
            }
          }
        });
      });
      // create a map that's centered on the selected adddress' location 
      map = new google.maps.Map(document.getElementById('map'), {
        center: city.geometry.location,
        zoom: 10
      });
      // Set the viewport on the selected adddress' viewport
      map.fitBounds(city.geometry.viewport);

    } else {
      alert('Geocode was not successful for the following reason: ' + status);
    }
  });
}

请找到工作示例here

【讨论】:

  • 嗨@annkylah,非常感谢您的回答。实际上,这个问题是要向谷歌地图 API 团队提出的。我希望谷歌地图 API 在未设置“中心”时显示用户的城市,因为这是我们使用谷歌地图的确切行为(当然由 IP 检测)。实现它对我们来说既困难又乏味。
  • 对于实施问题,我们通常将客户重定向到此处(StackOverflow),以便社区也可以提供帮助。但是,如果您遇到任何问题或对 Google Maps Platform 产品有功能要求,我建议您在 our Google Issue Tracker 中提交条目。请确保您选择了正确的组件(在您的情况下,该组件位于“JavaScript API”组件上)。为方便起见,这里是 form 的快速链接。
猜你喜欢
  • 2012-08-23
  • 2014-01-10
  • 2012-12-13
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多