【问题标题】:Google geocoding: Place Id are different when geocoding business/company name and addressGoogle 地理编码:对企业/公司名称和地址进行地理编码时,地点 ID 不同
【发布时间】:2017-12-17 23:03:54
【问题描述】:

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


按纬度/经度查找公司名称:我需要根据职位(纬度/经度)获取公司名称。

所以我尝试对纬度/经度进行地理编码以获取地点 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 不包含任何有关业务的内容。


按地址查找公司名称: 所以我尝试对地址进行地理编码,而不是经纬度: 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 中提供公司名称时,我得到了第三名: 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”。


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

【问题讨论】:

  • 反向地理编码器用于检索邮政地址。如果您需要公司名称,请使用 Places API。
  • 当我获得地点 id 时,我已经在地点 api 中查找了
  • 在地点上搜索地址不会改变任何东西...maps.googleapis.com/maps/api/place/textsearch/…
  • 要获得我想要的商家的地点,您需要搜索商家名称(或附近搜索坐标)
  • 你好。我的问题是我没有公司名称。所以我没有那个要搜索..

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


【解决方案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);

返回一个结果:

包豪斯希勒罗德
placeId:ChIJZWxjtmZAUkYRWY4K-RTjwsk

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

proof of concept fiddle

代码 sn-p:

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>

【讨论】:

    猜你喜欢
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多