【问题标题】:Google places api closes supermarketGoogle Places API 关闭超市
【发布时间】:2018-10-12 17:33:50
【问题描述】:

我知道我可以像这样使用 google api 找到关闭的超市:

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

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

【问题讨论】:

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


    【解决方案1】:

    您可以将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 个结果!
    • 那么您可以在结果数组中找到最短距离。你必须找到两点(位置)之间的距离。看看stackoverflow.com/questions/1502590/…
    【解决方案2】:

    这对我有用

      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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-14
      • 1970-01-01
      • 2015-12-04
      • 2016-05-02
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      相关资源
      最近更新 更多