Google Places API 关闭超市

3

我知道可以使用Google API像这样找到最近的超市:

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

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

2个回答

3
您可以使用distance参数和rankby选项。
var request = {
  location: gps,
  types: ['grocery'],
  rankBy: google.maps.places.RankBy.DISTANCE, 
  key: key 
};

参考资料:https://developers.google.com/places/web-service/search 当您有多个位置时,可以使用“Haversine公式”来获取两个位置之间的距离。
 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

谢谢,但我仍然得到多个结果。我只想要一个结果! - Jenssen
然后,您可以从结果数组中找到最短距离。您必须找到两点(位置)之间的距离。请查看https://dev59.com/J3I_5IYBdhLWcg3wK_o5 - Shiv Kumar Baghel

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 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接