我可以从Google地图自动填写中获得唯一的迪拜城市地址吗

我试过这个,但它也显示了Abu Dhabi位置,这是迪拜。但我只想在Dubai区域的地址。

我试过这个,但它也显示了Abu Dhabi位置,这是迪拜。但我只想在Dubai区域的地址。

var defaultBounds = new google.maps.LatLngBounds(
    new google.maps.LatLng(25.1851804, 55.2780796) // Dubai area
);
var options = {
    bounds: defaultBounds,
    types: ['establishment'],
    componentRestrictions: { country: 'AE' }
};
var autocomplete = new google.maps.places.Autocomplete(input, options);
3

Autocomplete唯一真正的选择是使用componentRestrictions到国家(就像您正在做的那样),然后使用strictBounds

对于迪拜的边界,地理编码器返回边界和视口:{ north:25.3585607, east:55.5650393, south:24.7921359, west:54.8904543}(自动完成 API 返回相同的视口,在place.geometry.viewport中)。

Bounds from Google Geocoder (square) vs. Bounds from GADM (not square): bounds of Dubai from Google's Geocoder vs. from GADM

proof of concept fiddle

screenshot of result

代码段:

// This example requires the Places library. Include the libraries=places
// parameter when you first load the API. For example:
// <script src="https://maps.googlea.com/maps/api/js?key=YOUR_API_KEY&libraries=places">
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    center: {
      lat: -33.8688,
      lng: 151.2195
    },
    zoom: 13
  });
  var input = document.getElementById('pac-input');
  var dubaiBounds = {
    north: 25.3585607,
    east: 55.5650393,
    south: 24.7921359,
    west: 54.8904543
  };
  map.fitBounds(dubaiBounds);
  var rect = new google.maps.Rectangle({
    map: map,
    bounds: dubaiBounds
  })
  var options = {
    bounds: dubaiBounds,
    types: ['establishment'],
    componentRestrictions: {
      country: 'AE'
    },
    strictBounds: true
  };
  var autocomplete = new google.maps.places.Autocomplete(input, options);
  /// show on map
  autocomplete.bindTo('bounds', map);
  // Set the data fields to return when the user selects a place.
  autocomplete.setFields(
    ['address_components', 'geometry', 'icon', 'name']);
  var infowindow = new google.maps.InfoWindow();
  var infowindowContent = document.getElementById('infowindow-content');
  infowindow.setContent(infowindowContent);
  var marker = new google.maps.Marker({
    map: map,
    anchorPoint: new google.maps.Point(0, -29)
  });
  autocomplete.addListener('place_changed', function() {
    infowindow.close();
    marker.setVisible(false);
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      // User entered the name of a Place that was not suggested and
      // pressed the Enter key, or the Place Details request failed.
      window.alert("No details available for input: '" + place.name + "'");
      return;
    }
    marker.setPosition(place.geometry.location);
    marker.setVisible(true);
    var address = '';
    if (place.address_components) {
      address = [
        (place.address_components[0] && place.address_components[0].short_name || ''),
        (place.address_components[1] && place.address_components[1].short_name || ''),
        (place.address_components[2] && place.address_components[2].short_name || '')
      ].join(' ');
    }
    infowindowContent.children['place-icon'].src = place.icon;
    infowindowContent.children['place-name'].textContent = place.name;
    infowindowContent.children['place-address'].textContent = address;
    infowindow.open(map, marker);
  });
}
/* Always set the map height explicitly to define the size of the div
 * element that contains the map. */
#map {
  height: 80%;
}
/* Optional: Makes the sample page fill the window. */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
#description {
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
}
#infowindow-content .title {
  font-weight: bold;
}
#infowindow-content {
  display: none;
}
#map #infowindow-content {
  display: inline;
}
.pac-card {
  margin: 10px 10px 0 0;
  border-radius: 2px 0 0 2px;
  box-sizing: border-box;
  -moz-box-sizing: border-box;
  outline: none;
  box-: 0 2px 6px rgba(0, 0, 0, 0.3);
  background-color: #fff;
  font-family: Roboto;
}
#pac-container {
  padding-bottom: 12px;
  margin-right: 12px;
}
.pac-controls {
  display: inline-block;
  padding: 5px 11px;
}
.pac-controls label {
  font-family: Roboto;
  font-size: 13px;
  font-weight: 300;
}
#pac-input {
  background-color: #fff;
  font-family: Roboto;
  font-size: 15px;
  font-weight: 300;
  margin-left: 12px;
  padding: 0 11px 0 13px;
  text-overflow: ellipsis;
  width: 400px;
}
#pac-input:focus {
  border-color: #4d90fe;
}
#title {
  color: #fff;
  background-color: #4d90fe;
  font-size: 25px;
  font-weight: 500;
  padding: 6px 12px;
}
<div id="pac-container">
  <input id="pac-input" type="text" placeholder="Enter a location">
</div>
<div id="map"></div>
<div id="infowindow-content">
  <img src="" width="16" height="16" id="place-icon">
  <span id="place-name" class="title"></span><br>
  <span id="place-address"></span>
</div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googlea.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&callback=initMap" async defer></script>
-2

我认为在你的情况下,你需要传递中心参数,例如:

center: new google.maps.LatLng(25.1851804, 55.2780796),

的链接有一个很好的例子:

https://www.webucator.com/how-to/how-use-latlngbounds-google-maps.cfm

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(756)
golang测试断言函数的 nil返回失败
上一篇
我可以从Google地图自动填写中获得唯一的迪拜城市地址吗
下一篇

相关推荐

  • google浏览器打不开网页是怎么回事:Google浏览器无法打开网页的原因及解决方法

    解决方案解决方案Google浏览器打不开网页可能是由于以下原因导致的:网络连接问题:由于网络连接问题,可能会影响Google浏览器访问网页的功能。可以尝试重新连接网络,或者使用其他浏览器来访问网页,来检查是否是网络连接问题。…

    2023-07-04 08:19:04
    0 54 98
  • Ios韩国账号:造型的 Google韩国地图

    关于Ios韩国账号的问题,在google maps korea中经常遇到,我有一个网站,在那里我使用了一个带样式的谷歌地图。如果你把地图移到韩国,没有地图特征 (道路、高速公路、地方),只有命名的城市和一些城市边界。如果我删除样式,所有这些功能都会恢复。…

    2022-12-22 03:47:36
    0 57 61
  • Vr怎么玩:适用于三星GearVR的GoogleVR SDK

    关于Vr怎么玩的问题,在gearvr oculus中经常遇到,Google VR SDK 是否可以在 Samsung Gear VR(Oculus)上运行,或者我是否绑定到 Oculus SDK?或者是否有任何其他替代方案(最好是开源的)…

    2022-12-18 13:21:22
    0 78 59
  • 仿爱聊源码:Google环聊-邀请

    关于仿爱聊源码的问题,在how to send a google hangout invite中经常遇到,这是我的环聊按钮:…

    2022-12-06 13:33:05
    0 36 15
  • 小程序游戏排行榜:Google Play游戏的成就和排行榜

    关于小程序游戏排行榜的问题,在google achievements中经常遇到,我做了 2 个游戏,第一个是[Hangman PlayStoreLink][code GitHubLink],最近的是[Reactions PlayStoreLink][code GitHubLink]。…

    2022-12-06 09:23:17
    0 83 11
  • 如何查看小程序的网址:如何在Google中索引重定向的网址:短网址

    关于如何查看小程序的网址的问题,在shorturl at中经常遇到,我正在研究新项目“短 URL”。我成功创建了一个短网址,但是当我向谷歌“网站管理员工具”提交一个短网址时,它会给我一个重定向错误。…

    2022-12-06 10:03:34
    0 40 22
  • Js弹窗广告代码:Google广告通过节点JS的广告api增强了转换

    关于Js弹窗广告代码的问题,在enhanced conversions google ads中经常遇到,我正在尝试实现 Google 广告增强转换。我可以选择使用 gtag,google tag manager 或使用 Ads api。但是,在 Ads api 上我没有看到任何与 Node Js 相关的文档。我无法弄清楚如何利用 Ads api 与 Node Js 实现增强转换。…

    2022-12-22 11:47:23
    0 58 82
  • Wps表格怎么换行:Google表格表格图表-换行文本

    关于Wps表格怎么换行的问题,在how to wrap text in sheets中经常遇到,我有一个表格图表,在我的一个单元格中有很多文本,我正在引用,而不是将文本包装在源单元格中,它只是在表格图表中放置一个滚动条。有人知道是否有一种将文本包装在图形中的方法吗?我一直在网上寻找,但似乎是关于这些图表的很少的文档。…

    2022-12-11 11:01:39
    0 44 81

发表评论

登录 后才能评论

评论列表(34条)