【问题标题】:Google API: Counting Location ResultsGoogle API:计算位置结果
【发布时间】:2019-11-12 02:15:26
【问题描述】:

我正在尝试对搜索附近餐馆的 API 进行哪些改进。我想做的是创建一个文本显示来告诉用户找到了多少个位置,但是我没有找到任何可以用作如何执行此操作的参考点的东西。有没有办法将位置搜索的搜索结果变成总数?我假设我需要有某种柜台,但我不确定。作为参考,这里是原始代码。

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Place search pagination</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;
      }
      #right-panel {
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }

      #right-panel select, #right-panel input {
        font-size: 15px;
      }

      #right-panel select {
        width: 100%;
      }

      #right-panel i {
        font-size: 12px;
      }
      #right-panel {
        font-family: Arial, Helvetica, sans-serif;
        position: absolute;
        right: 5px;
        top: 60%;
        margin-top: -195px;
        height: 330px;
        width: 200px;
        padding: 5px;
        z-index: 5;
        border: 1px solid #999;
        background: #fff;
      }
      h2 {
        font-size: 22px;
        margin: 0 0 5px 0;
      }
      ul {
        list-style-type: none;
        padding: 0;
        margin: 0;
        height: 271px;
        width: 200px;
        overflow-y: scroll;
      }
      li {
        background-color: #f1f1f1;
        padding: 10px;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
      li:nth-child(odd) {
        background-color: #fcfcfc;
      }
      #more {
        width: 100%;
        margin: 5px 0 0 0;
      }
    </style>
    <script>
      // 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">

      var map;

      function initMap() {
        // Create the map.
        var pyrmont = {lat: -33.866, lng: 151.196};
        map = new google.maps.Map(document.getElementById('map'), {
          center: pyrmont,
          zoom: 17
        });

        // Create the places service.
        var service = new google.maps.places.PlacesService(map);
        var getNextPage = null;
        var moreButton = document.getElementById('more');
        moreButton.onclick = function() {
          moreButton.disabled = true;
          if (getNextPage) getNextPage();
        };

        // Perform a nearby search.
        service.nearbySearch(
            {location: pyrmont, radius: 500, type: ['restaurants']},
            function(results, status, pagination) {
              if (status !== 'OK') return;

              createMarkers(results);
              moreButton.disabled = !pagination.hasNextPage;
              getNextPage = pagination.hasNextPage && function() {
                pagination.nextPage();
              };
            });
      }

      function createMarkers(places) {
        var bounds = new google.maps.LatLngBounds();
        var placesList = document.getElementById('places');

        for (var i = 0, place; place = places[i]; i++) {
          var image = {
            url: place.icon,
            size: new google.maps.Size(71, 71),
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(17, 34),
            scaledSize: new google.maps.Size(25, 25)
          };

          var marker = new google.maps.Marker({
            map: map,

            title: place.name,
            position: place.geometry.location
          });

          var li = document.createElement('li');
          li.textContent = place.name;
          placesList.appendChild(li);

          bounds.extend(place.geometry.location);
        }
        map.fitBounds(bounds);
      }
    </script>
  </head>
  <body>
    <div id="map"></div>
    <div id="right-panel">
      <h2>Results</h2>
      <ul id="places"></ul>
      <button id="more">More results</button>
    </div>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBYoCkx3owRBY3uLwBvW36B0GNeMQtBm1o&libraries=places&callback=initMap" async defer></script>
  </body>
</html>

【问题讨论】:

    标签: html api google-maps geolocation counter


    【解决方案1】:

    最简单的使用placesList.children的长度。添加一个放置号码的地方:

    <div id="right-panel">
      <h2>Results</h2>
      <div id="number_results"></div>
      <ul id="places"></ul>
      <button id="more">More results</button>
    </div>
    

    然后在javascript中,将placesList.children.length放在那里:

    var li = document.createElement('li');
    li.textContent = place.name;
    placesList.appendChild(li);
    document.getElementById('number_results').innerHTML = placesList.children.length+" returned";
    

    代码 sn-p:

    // 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">
    
    var map;
    
    function initMap() {
      // Create the map.
      var pyrmont = {
        lat: -33.866,
        lng: 151.196
      };
      map = new google.maps.Map(document.getElementById('map'), {
        center: pyrmont,
        zoom: 17
      });
    
      // Create the places service.
      var service = new google.maps.places.PlacesService(map);
      var getNextPage = null;
      var moreButton = document.getElementById('more');
      moreButton.onclick = function() {
        moreButton.disabled = true;
        if (getNextPage) getNextPage();
      };
    
      // Perform a nearby search.
      service.nearbySearch({
          location: pyrmont,
          radius: 500,
          type: ['restaurants']
        },
        function(results, status, pagination) {
          if (status !== 'OK') return;
    
          createMarkers(results);
          moreButton.disabled = !pagination.hasNextPage;
          getNextPage = pagination.hasNextPage && function() {
            pagination.nextPage();
          };
        });
    }
    
    function createMarkers(places) {
      var bounds = new google.maps.LatLngBounds();
      var placesList = document.getElementById('places');
    
      for (var i = 0, place; place = places[i]; i++) {
        var image = {
          url: place.icon,
          size: new google.maps.Size(71, 71),
          origin: new google.maps.Point(0, 0),
          anchor: new google.maps.Point(17, 34),
          scaledSize: new google.maps.Size(25, 25)
        };
    
        var marker = new google.maps.Marker({
          map: map,
          title: place.name,
          position: place.geometry.location
        });
    
        var li = document.createElement('li');
        li.textContent = place.name;
        placesList.appendChild(li);
        document.getElementById('number_results').innerHTML = placesList.children.length + " returned";
    
        bounds.extend(place.geometry.location);
      }
      map.fitBounds(bounds);
    }
    /* 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;
    }
    
    #right-panel {
      font-family: 'Roboto', 'sans-serif';
      line-height: 30px;
      padding-left: 10px;
    }
    
    #right-panel select,
    #right-panel input {
      font-size: 15px;
    }
    
    #right-panel select {
      width: 100%;
    }
    
    #right-panel i {
      font-size: 12px;
    }
    
    #right-panel {
      font-family: Arial, Helvetica, sans-serif;
      position: absolute;
      right: 5px;
      top: 60%;
      margin-top: -195px;
      height: 330px;
      width: 200px;
      padding: 5px;
      z-index: 5;
      border: 1px solid #999;
      background: #fff;
    }
    
    h2 {
      font-size: 22px;
      margin: 0 0 5px 0;
    }
    
    ul {
      list-style-type: none;
      padding: 0;
      margin: 0;
      height: 271px;
      width: 200px;
      overflow-y: scroll;
    }
    
    li {
      background-color: #f1f1f1;
      padding: 10px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
    
    li:nth-child(odd) {
      background-color: #fcfcfc;
    }
    
    #more {
      width: 100%;
      margin: 5px 0 0 0;
    }
    <div id="map"></div>
    <div id="right-panel">
      <h2>Results</h2>
      <div id="number_results"></div>
      <ul id="places"></ul>
      <button id="more">More results</button>
    </div>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBYoCkx3owRBY3uLwBvW36B0GNeMQtBm1o&libraries=places&callback=initMap" async defer></script>

    【讨论】:

      猜你喜欢
      • 2018-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-05
      • 2019-12-14
      • 1970-01-01
      相关资源
      最近更新 更多