Javascript 谷歌placeId查找器

Javascript 谷歌placeId查找器,javascript,html,google-maps,web-services,google-places-api,Javascript,Html,Google Maps,Web Services,Google Places Api,我想在我的网站上显示“查找”工具,用户在其中搜索他的谷歌注册名称,谷歌提供他的位置ID作为回应?可以在中找到一个工作示例。完整代码如下: <!DOCTYPE html> <html> <head> <title>Place ID Finder</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> &

我想在我的网站上显示“查找”工具,用户在其中搜索他的谷歌注册名称,谷歌提供他的位置ID作为回应?

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

<!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>

位置ID查找器
/*始终明确设置贴图高度以定义div的大小
*包含映射的元素*/
#地图{
身高:100%;
}
/*可选:使示例页面填充窗口*/
html,正文{
身高:100%;
保证金:0;
填充:0;
}
.控制{
背景色:#fff;
边界半径:2px;
边框:1px实心透明;
盒影:0 2px 6px rgba(0,0,0,0.3);
框大小:边框框;
字体系列:Roboto;
字体大小:15px;
字体大小:300;
高度:29px;
左边距:17px;
边缘顶部:10px;
大纲:无;
填充:0 11px 0 13px;
文本溢出:省略号;
宽度:400px;
}
.控制:焦点{
边框颜色:#4d90fe;
}
.头衔{
字体大小:粗体;
}
#信息窗口内容{
显示:无;
}
#地图#信息窗口内容{
显示:内联;
}

地点ID:
//此示例使用PlaceAutoComplete小部件允许用户进行搜索 //查找并选择一个位置。然后,该示例显示一个信息窗口,其中包含 //用户拥有的地点ID和有关该地点的其他信息 //选中。 //此示例需要Places库。包括图书馆=地方 //第一次加载API时的参数。例如: // 函数initMap(){ var map=new google.maps.map(document.getElementById('map'){ 中心:{lat:-33.8688,lng:151.2195}, 缩放:13 }); var input=document.getElementById('pac-input'); var autocomplete=new google.maps.places.autocomplete(输入); autocomplete.bindTo('bounds',map); //仅指定所需的位置数据字段。 设置字段(['place_id','geometry','name']); map.controls[google.maps.ControlPosition.TOP_LEFT].push(输入); var infowindow=new google.maps.infowindow(); var infowindowContent=document.getElementById('infowindow-content'); setContent(infowindowContent); var marker=new google.maps.marker({map:map}); marker.addListener('click',function()){ 信息窗口。打开(地图、标记); }); autocomplete.addListener('place\u changed',function(){ infowindow.close(); var place=autocomplete.getPlace(); 如果(!place.geometry){ 返回; } if(place.geometry.viewport){ map.fitBounds(place.geometry.viewport); }否则{ 地图。设置中心(地点。几何。位置); map.setZoom(17); } //使用位置ID和位置设置标记的位置。 标记。设定地点({ 地点id:place.place\u id, 位置:place.geometry.location }); marker.setVisible(true); infowindowContent.children['place-name'].textContent=place.name; infowindowContent.children['place-id'].textContent=place.place\u id; infowindowContent.children['place-address'].textContent= place.u地址; 信息窗口。打开(地图、标记); }); }

您需要自己的API密钥,因此请确保通过谷歌的API正确设置您的项目和账单帐户

希望这有帮助