使用Google Maps API查找与街道地址相关的公司名称?

时间:2018-07-23 17:06:33

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

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

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

例如,当搜索“德克萨斯州奥斯丁的West Anderson Ln 400”时,我想知道“ Austin Parke Apartments”位于此地址。我该怎么办?

1 个答案:

答案 0 :(得分:2)

  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

screenshot of resulting map

代码段:

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>