谷歌Place api关闭超市

时间:2018-10-12 17:33:51

标签: google-api google-places-api distance

我知道我可以使用Google API找到关闭超市的地方:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=51.360229, 6.042169&radius=3000&type=supermarket&key=myKey

但是现在我得到了很多结果。我如何只获得最接近的一个?

2 个答案:

答案 0 :(得分:2)

您可以将rankby选项与distance参数一起使用

var request = {
  location: gps,
  types: ['grocery'],
  rankBy: google.maps.places.RankBy.DISTANCE, 
  key: key 
};

引用:https://developers.google.com/places/web-service/search

一旦您有多个位置,请使用haversine formula获取两个位置之间的距离。

 function distance(p1, p2) {
      if (!p1 || !p2) 
       return 0;
      var R = 6371000; // Radius of the Earth in m
      var dLat = (p2.lat() - p1.lat()) * Math.PI / 180;
      var dLon = (p2.lng() - p1.lng()) * Math.PI / 180;
      var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
      Math.cos(p1.lat() * Math.PI / 180) * Math.cos(p2.lat() * Math.PI / 180) *
      Math.sin(dLon / 2) * Math.sin(dLon / 2);
      var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
      var d = R * c;
      return d;
     }

引用:https://www.movable-type.co.uk/scripts/latlong.html

答案 1 :(得分:0)

这对我有用

  https://maps.googleapis.com/maps/api/place/nearbysearch/json? 
     location=51.360229,6.042169&radius=3000&type=grocery&key=vaquarkhan&rankBy? 
        google.maps.places.RankBy.DISTANCE