如何限制googlemap只显示特定城市?

时间:2017-08-01 03:28:09

标签: javascript google-maps

我想在我的网站上只显示使用GoogleMap的某些地方。它不应该显示任何小地方。只有主要地方应该可以在地图上看到。获取此功能的程序是什么?

1 个答案:

答案 0 :(得分:-1)

将您的lat / lon设置为邮局(或其他城市指示符)的位置。 然后,使用zoom属性修复要包含的级别。

请参阅:https://developers.google.com/maps/documentation/javascript/examples/marker-simple

以下是示例的代码:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple markers</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
    </style>
  </head>
  <body>
    <div id="map"></div>
    <script>

      function initMap() {
        var myLatLng = {lat: -25.363, lng: 131.044};

        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: myLatLng
        });

        var marker = new google.maps.Marker({
          position: myLatLng,
          map: map,
          title: 'Hello World!'
        });
      }
    </script>
    <script async defer
    src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap">
    </script>
  </body>
</html>

在map.zoom = 16处尝试该脚本。应该靠近城市景观。伦敦我认为。

您也可以设置地图边界,但这要困难得多。您需要找到您的位置,然后在该区域周围绘制几何图形,定义点,然后将所有点缩放到视图中。 所有这一切都是可能的,但你没有向我展示任何代码,所以我没有展示任何代码。

希望你找到有用的东西,

相关问题