【问题标题】:How to get a Google Place ID from a Google GMS Address如何从 Google GMS 地址获取 Google Place ID
【发布时间】:2016-11-23 03:37:46
【问题描述】:

我看过以前关于从 Google Place ID 转换为地址的帖子;但是我对相反的事情感兴趣。

我有所需位置的 GMS 地址,但我想获取 Google Place ID 以便向用户展示更多详细信息。根据我在 Google 的 iOS API 网站上看到的内容,您可以通过自动完成功能(用于用户搜索)、选定的位置功能或当前位置功能获得 Google Place ID。

我的应用程序打算显示我已经拥有地址的多个位置。除了在到达时收到详细信息外,用户还可以选择各个位置以获取更多详细信息。因此,上面列出的功能并不理想。

我也尝试使用具有“文本搜索请求”和“附近搜索请求”等功能的 Google 的 Web API,但是我收到“零结果”。

还有其他我没有想到和/或见过的方法吗?

【问题讨论】:

    标签: ios swift google-maps-api-3 google-places-api


    【解决方案1】:

    Google Maps JavaScript API 中的Place ID finder 怎么样。

    place ID 是唯一标识地点的文本标识符。它也适用于大多数地点,包括企业、地标、公园和十字路口。这些 ID 是稳定的,这意味着一旦您确定了某个地点的地点 ID,您可以在下次查找该地点时重复使用该值。

    您可以在 Google Places API 和许多 Google Maps API 中使用相同的地点 ID。例如,您可以在 Places APIGoogle Maps JavaScript APIGoogle Maps Geocoding API 中使用相同的地点 ID 来引用地点>、Google Maps Embed APIGoogle Maps Roads API

    这是用于此的示例代码。

    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 autocomplete = new google.maps.places.Autocomplete(input);
            autocomplete.bindTo('bounds', map);
    
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
    
            var infowindow = new google.maps.InfoWindow();
            var marker = new google.maps.Marker({
              map: map
            });
            marker.addListener('click', function() {
              infowindow.open(map, marker);
            });
    
            autocomplete.addListener('place_changed', function() {
              infowindow.close();
              var place = autocomplete.getPlace();
              if (!place.geometry) {
                return;
              }
    
              if (place.geometry.viewport) {
                map.fitBounds(place.geometry.viewport);
              } else {
                map.setCenter(place.geometry.location);
                map.setZoom(17);
              }
    
              // Set the position of the marker using the place ID and location.
              marker.setPlace({
                placeId: place.place_id,
                location: place.geometry.location
              });
              marker.setVisible(true);
    
              infowindow.setContent('<div><strong>' + place.name + '</strong><br>' +
                  'Place ID: ' + place.place_id + '<br>' +
                  place.formatted_address);
              infowindow.open(map, marker);
            });
          }
    

    请注意,此示例需要 Places 库。包括 首次加载 API 时的 libraries=places 参数。为了 示例:

    script src="https://maps.googleapis.com/maps/api/jskey=YOUR_API_KEY&amp;libraries=places"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2018-08-31
      • 2015-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多