首页 文章

Google地理编码:在对商家/公司名称和地址进行地理编码时,地点ID不同

提问于
浏览
0

My Problem: 我的情况是,在我的应用程序中,我有一个输入字段,您可以在其中搜索使用自动完成的地址 . 在此字段中,您可以键入任何公司名称,它将找到lat / long并在输入下方的 Map 上设置标记 . 然后用户也可以使用 Map 将标记拖放到某处,这就出现了问题,因为我无法通过这种方式获得公司名称,这在我的要求中是必需的 . 我失败的尝试在下面 .


Finding company name by lat/lon: 我需要根据位置(纬度/经度)得到公司名称 .

所以我尝试的是对lat / lon进行地理编码以获得地点ID:https://maps.googleapis.com/maps/api/geocode/json?latlng=55.93366899999999,12.258698299999992&key=xxx

它给了我place_id:“ChIJwSWuIGRAUkYRf7LPSwRStYU”

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

但没有地名! JSON不包含任何有关业务的内容 .


Finding company name by address: 所以我试图对地址进行地理编码而不是lat / long: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

把它扔到地方后,这个地方和以前一样没有公司名称 .


Finding company name by "company name": 但是当我尝试在网址中提供公司名称时,我获得了第三名:https://maps.googleapis.com/maps/api/geocode/json?address=Bauhaus%20Hillerød

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

这个地方ID工作,我得到公司名称“BAUHAUS” .


Question: 那么如何在不知情的情况下获取公司名称?对我来说,一个地方可以有不同的地方ID似乎也很疯狂 .

1 回答

  • 1

    在该位置使用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);
    

    返回一个结果:

    BAUHAUSHillerødplaceId:ChIJZWxjtmZAUkYRWY4K-RTjwsk

    (注意:没有 type:"store" ,我会得到不同的结果)

    proof of concept fiddle

    screenshot

    code snippet:

    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>
    

相关问题