代码之家  ›  专栏  ›  技术社区  ›  Michael Winther

谷歌地理编码:对企业/公司名称和地址进行地理编码时,地点Id不同

  •  0
  • Michael Winther  · 技术社区  · 7 年前

    我的问题: 我的场景是,在我的应用程序中,我有一个输入字段,在那里你可以搜索使用自动完成的地址。在此字段中,您可以键入任何公司名称,它将找到纬度/经度,并在输入下方的地图上设置标记。然后用户也可以使用地图将标记拖放到某个地方,问题来了,因为我无法通过这种方式获得公司名称,这是我的需求中所需要的。我失败的尝试如下。


    所以我试着对纬度/经度进行地理编码,以获得地点id: https://maps.googleapis.com/maps/api/geocode/json?latlng=55.93366899999999,12.258698299999992&key=xxx

    然后我用它来获得地名: https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJwSWuIGRAUkYRf7LPSwRStYU&key=xxx


    https://maps.googleapis.com/maps/api/geocode/json?address=Sigrunsvej%2040%20Hillerød

    这给了我位置id:ChIJa1mGSGFAUkYR2SpylJRKlLM https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJa1mGSGFAUkYR2SpylJRKlLM&key=xxx


    但当我试图在url中提供公司名称时,我得到了一个第三位的id: https://maps.googleapis.com/maps/api/geocode/json?address=Bauhaus%20Hillerød

    https://maps.googleapis.com/maps/api/place/details/json?placeid=ChIJZWxjtmZAUkYRWY4K-RTjwsk&key=xxx

    这个地方的id工作,我得到的公司名称“包豪斯”。


    问题: 那么,我如何在不知道公司名称的情况下获得公司名称呢?在我看来,一个地方可以有不同的地方ID也很疯狂。

    1 回复  |  直到 7 年前
        1
  •  1
  •   geocodezip    7 年前

    使用 nearbySearch

    var service = new google.maps.places.PlacesService(map);
    var radius = 100;
    service.nearbySearch({
      location: new google.maps.LatLng(55.93366899999999, 12.258698299999992),
      radius: radius,
      type: "store"
    }, callback);
    

    返回一个结果:

    包豪斯·希勒德

    (注:无 type:"store"

    proof of concept fiddle

    screenshot

    var infowindow;
    var map;
    
    function initialize() {
      map = new google.maps.Map(
        document.getElementById("map_canvas"), {
          center: new google.maps.LatLng(55.93366899999999, 12.258698299999992),
          zoom: 13,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        });
      infowindow = new google.maps.InfoWindow();
      var service = new google.maps.places.PlacesService(map);
      var radius = 100;
      var circle = new google.maps.Circle({
        map: map,
        center: map.getCenter(),
        radius: radius
      });
      map.fitBounds(circle.getBounds());
      service.nearbySearch({
        location: map.getCenter(),
        radius: radius,
        type: "store"
      }, callback);
    }
    
    function callback(results, status) {
      if (status === google.maps.places.PlacesServiceStatus.OK) {
        console.log(results.length + " results");
        for (var i = 0; i < results.length; i++) {
          createMarker(results[i]);
        }
      } else console.log("status=" + status);
    }
    
    function createMarker(place) {
      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>placeId:" + place.place_id);
        infowindow.open(map, this);
      });
      google.maps.event.trigger(marker, 'click');
    }
    
    
    google.maps.event.addDomListener(window, "load", initialize);
    html,
    body,
    #map_canvas {
      height: 100%;
      width: 100%;
      margin: 0px;
      padding: 0px
    }
    <script src="https://maps.googleapis.com/maps/api/js?libraries=places"></script>
    <div id="map_canvas"></div>