【问题标题】:How to sort results by distance with google places api using Nearby Search Requests如何使用 Google Places api 使用附近搜索请求按距离对结果进行排序
【发布时间】:2013-03-25 00:53:24
【问题描述】:

我想按距离获取我所在位置附近的所有机场。我正在使用附近的搜索请求和谷歌地方 api 使用这个 url:https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location=51.9143924,-0.1640153&sensor=true&key=api_key&radius=50000&types=airport 我得到的结果是稀疏的,没有任何顺序。我试过 rankby=distance 但没有结果出现。 并且根据 https://developers.google.com/places/documentation/search#PlaceSearchRequests 文档“如果 rankby=distance”,则不得包含半径。

【问题讨论】:

    标签: api google-maps google-places-api google-places


    【解决方案1】:

    是的,您不能在同一请求中使用 radiusrankBy。但是你可以使用rankBy=distance,然后根据geometry.location.latgeometry.location.lng自己计算距离。例如在 groovy 中我已经这样做了:

    GeoPositionPlaceclasses 是我实现的,所以不要指望在核心库中找到它们 :)

    TreeMap<Double, Place> nearbyPlaces = new TreeMap<Double, Place>()
    
     if(isStatusOk(nearbySearchResponse))
                        nearbySearchResponse.results.each {
    
                    def location = it.geometry.location
                    String placeid = it.place_id
                    GeoPosition position = new GeoPosition(latitude: location.lat,
                            longitude: location.lng)
    
                    Place place =  new Place(position)
    
                    double distance = distanceTo(place)
    //If the place is actually in your radius (because Places API oftenly returns places far beyond your radius) then you add it to the TreeMap with distance to it as a key, and it will automatically sort it for you.
    
                    if((distance <= placeSearcher.Radius()))
                        nearbyPlaces.put(distance, place)
    
                }
    

    这样计算距离的地方(Haversine 公式):

    public double distanceTo(GeoPosition anotherPos){
    
        int EARTH_RADIUS_KM = 6371;
        double lat1Rad = Math.toRadians(this.latitude);
        double lat2Rad = Math.toRadians(anotherPos.latitude);
        double deltaLonRad = Math.toRadians(anotherPos.longitude - this.longitude);
    
        return 1000*Math.acos(
                            Math.sin(lat1Rad) * Math.sin(lat2Rad) +
                            Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)
                        ) * EARTH_RADIUS_KM;
    }
    

    【讨论】:

      【解决方案2】:

      你不能同时使用radiusrankby 这是问题所在

      【讨论】:

        【解决方案3】:

        截至 2018 年 10 月,Google 已将初始位置添加为附近搜索的一部分,如下所示:

        service.nearbySearch({
          location: place.geometry.location, //Add initial lat/lon here
          rankBy: google.maps.places.RankBy.DISTANCE,
          type: ['museum']
        }, callback);
        

        上述代码将返回按距离 asc 排序的指定位置附近的博物馆。 在此处查找更多信息:https://developers.google.com/maps/documentation/javascript/examples/place-search

        【讨论】:

          猜你喜欢
          • 2014-05-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-12
          • 2011-12-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多