【问题标题】:Google placeId FinderGoogle placeId 查找器
【发布时间】:2020-02-19 06:24:57
【问题描述】:

我想在我的网站上显示 find PlaceID 工具,用户可以在其中搜索他的谷歌注册名称并作为响应谷歌提供他的地点 ID?

【问题讨论】:

    标签: javascript html google-maps web-services google-places-api


    【解决方案1】:

    可以在this link 中找到一个工作示例。完整代码如下:

    <!DOCTYPE html>
    <html>
      <head>
        <title>Place ID Finder</title>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <meta charset="utf-8">
        <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;
          }
          .controls {
            background-color: #fff;
            border-radius: 2px;
            border: 1px solid transparent;
            box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
            box-sizing: border-box;
            font-family: Roboto;
            font-size: 15px;
            font-weight: 300;
            height: 29px;
            margin-left: 17px;
            margin-top: 10px;
            outline: none;
            padding: 0 11px 0 13px;
            text-overflow: ellipsis;
            width: 400px;
          }
    
          .controls:focus {
            border-color: #4d90fe;
          }
          .title {
            font-weight: bold;
          }
          #infowindow-content {
            display: none;
          }
          #map #infowindow-content {
            display: inline;
          }
    
        </style>
      </head>
      <body>
        <div style="display: none">
            <input id="pac-input"
                   class="controls"
                   type="text"
                   placeholder="Enter a location">
        </div>
        <div id="map"></div>
        <div id="infowindow-content">
            <span id="place-name" class="title"></span><br>
            <strong>Place ID:</strong> <span id="place-id"></span><br>
            <span id="place-address"></span>
        </div>
    
        <script>
          // This sample uses the Place Autocomplete widget to allow the user to search
          // for and select a place. The sample then displays an info window containing
          // the place ID and other information about the place that the user has
          // selected.
    
          // This example requires the Places library. Include the libraries=places
          // parameter when you first load the API. For example:
          // <script src="https://maps.googleapis.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 autocomplete = new google.maps.places.Autocomplete(input);
            autocomplete.bindTo('bounds', map);
    
            // Specify just the place data fields that you need.
            autocomplete.setFields(['place_id', 'geometry', 'name']);
    
            map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
    
            var infowindow = new google.maps.InfoWindow();
            var infowindowContent = document.getElementById('infowindow-content');
            infowindow.setContent(infowindowContent);
    
            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);
    
              infowindowContent.children['place-name'].textContent = place.name;
              infowindowContent.children['place-id'].textContent = place.place_id;
              infowindowContent.children['place-address'].textContent =
                  place.formatted_address;
              infowindow.open(map, marker);
            });
          }
        </script>
        <script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&libraries=places&callback=initMap"
            async defer></script>
      </body>
    

    您需要自己的 API 密钥,因此请务必通过 Google 的 get started guide 正确设置您的项目和结算帐户。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2016-10-05
      • 1970-01-01
      • 2016-11-29
      • 2016-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多