2013-04-03 54 views
1

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

回答

0

不能使用radiusrankby在一起,这就是问题所在

1

是的,你不能在同一个请求使用半径rankBy。但你可以使用rankBy=distance然后根据geometry.location.latgeometry.location.lng来计算你自己的距离。对于常规〔实施例我所做的那样:

GeoPositionPlace类都是由我来实现,所以不要指望在核心库:)找他们

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; 
}