【问题标题】:Find the business name associated with a street address with the Google Maps API?使用 Google Maps API 查找与街道地址相关联的商家名称?
【发布时间】:2018-07-23 17:06:33
【问题描述】:

使用 Google Maps Platform (Web JavaScript API),我如何找到与街道地址关联的公司名称?

我希望 PlacesService.textSearch() 或 PlacesService.findPlaceFromQuery() 在通过与企业/机构关联的地址进行查询时返回“street_address”和“机构”类型。但是,当我按街道地址搜索时,我只会收到“street_address”结果。

例如,当搜索“400 West Anderson Ln, Austin, Texas”时,我想知道“Austin Parke Apartments”在这个地址。我该怎么做?

【问题讨论】:

    标签: google-maps google-maps-api-3


    【解决方案1】:
    1. 对地址进行地理编码
    2. 使用地点库nearbySearch 查找该地址附近的地点
    3. 选择最接近的结果
    var address = "400 West Anderson Ln, Austin, Texas";
    geocoder.geocode({
      address: address
    }, function(results, status) {
      if (status === 'OK') {
        map = new google.maps.Map(document.getElementById('map'), {
          center: results[0].geometry.location,
          zoom: 15
        });
        latLng = results[0].geometry.location;
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
          map: map,
          position: results[0].geometry.location
        });
        var service = new google.maps.places.PlacesService(map);
        service.nearbySearch({
          location: marker.getPosition(),
          rankBy: google.maps.places.RankBy.DISTANCE,
          types: ["establishment"]
        }, function(results, status) {
          if (status === google.maps.places.PlacesServiceStatus.OK) {
            for (var i = 0; i < results.length; i++) {
              var marker = createMarker(results[i], i);
              if (i == 0)
                google.maps.event.trigger(marker, 'click');
            }
          } else {
            alert('nearbySearch was not successful for the following reason: ' + status);
          }
        });
      } else {
        alert('Geocode was not successful for the following reason: ' + status);
      }
    })
    

    proof of concept fiddle

    代码 sn-p:

    var map;
    var infowindow;
    var latLng;
    var address = "400 West Anderson Ln, Austin, Texas";
    
    function initMap() {
      infowindow = new google.maps.InfoWindow();
      var geocoder = new google.maps.Geocoder();
      geocoder.geocode({
        address: address
      }, function(results, status) {
        if (status === 'OK') {
          map = new google.maps.Map(document.getElementById('map'), {
            center: results[0].geometry.location,
            zoom: 15
          });
          latLng = results[0].geometry.location;
          console.log(results[0].geometry.location.toUrlValue(6))
          map.setCenter(results[0].geometry.location);
          var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
          });
          var service = new google.maps.places.PlacesService(map);
          service.nearbySearch({
            location: marker.getPosition(),
            rankBy: google.maps.places.RankBy.DISTANCE,
            types: ["establishment"]
          }, function(results, status) {
            if (status === google.maps.places.PlacesServiceStatus.OK) {
              console.log(results.length + " results returned");
              for (var i = 0; i < results.length; i++) {
                var marker = createMarker(results[i], i);
                if (i == 0)
                  google.maps.event.trigger(marker, 'click');
                else 
                  marker.setMap(null);
              }
            } else {
              alert('nearbySearch was not successful for the following reason: ' + status);
            }
          });
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
      })
    }
    
    function createMarker(place, index) {
      var placeLoc = place.geometry.location;
      var marker = new google.maps.Marker({
        map: map,
        position: place.geometry.location
      });
    
      google.maps.event.addListener(marker, 'click', function() {
        infowindow.setContent(place.name + "<br>" + address + "<br>" + index);
        infowindow.open(map, this);
      });
      return marker;
    }
    #map {
      height: 100%;
    }
    
    html,
    body {
      height: 100%;
      margin: 0;
      padding: 0;
    }
    <div id="map"></div>
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places,geometry&callback=initMap" async defer></script>

    【讨论】:

    • 谢谢!这效果很好。我当然希望 Google 在初始请求时只返回给定地址的所有地方记录。
    • Google 已更改其结算结构,现在请注意不要使用 nearbySearch 从以下地址查找地点: 。 . . . . “默认情况下,当用户选择一个地点时,附近搜索会返回所选地点的所有可用数据字段,并且您将相应地付费。没有办法限制附近搜索请求只返回特定字段。为了避免请求(并支付)您不需要的数据,请改用Find Place 请求。”我想答案很容易适应。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 2013-03-22
    相关资源
    最近更新 更多