谷歌按距离排列api排名

时间:2013-03-27 18:11:51

标签: android gps google-places-api

如何使用rankBy = distance按距离排序地点列表,并在列表视图中显示该商家名称旁边的距离???

protected String doInBackground(String... args) {
    googlePlaces = new GooglePlaces();                      
    try {
        String types = null;
        double radius = 400;
        nearPlaces = googlePlaces.search(gps.getLatitude(),gps.getLongitude(), radius, types);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

2 个答案:

答案 0 :(得分:2)

我目前正在研究这个问题。

  1. 不包括rankBy =距离的“radius”参数,如Google Places API文档所述。
  2. 一旦你获得了回复的名单,你将不得不再打一次电话来获取每个地方的详细信息,这样你就可以获得地址和信息。每个人都有Lat / Lng。
  3. 然后使用当前位置,计算结果中每个地方的距离。

答案 1 :(得分:2)

您可以使用此搜索网址来实现您所描述的内容:

private static final String PLACES_SEARCH_URL = "https://maps.googleapis.com/maps/api/place/search/json?rankby=distance&";

public PlacesList search(double latitude, double longitude, double radius, String types) throws Exception {
    this._latitude = latitude;
    this._longitude = longitude;
    this._radius = radius;
    //this._rankby=_rankby;

    try {
        HttpRequestFactory httpRequestFactory = createRequestFactory(HTTP_TRANSPORT);
        HttpRequest request = httpRequestFactory.buildGetRequest(new GenericUrl(PLACES_SEARCH_URL));
        request.getUrl().put("key", API_KEY);
        request.getUrl().put("location", _latitude + "," + _longitude);
        //  request.getUrl().put("radius", _radius);
        request.getUrl().put("rankBy", _radius);
        // in meters
        request.getUrl().put("sensor", "false");
        //request.getUrl().put("rankby", _rankby);
        if(types != null) {
            request.getUrl().put("types", types);
        }

        PlacesList list = request.execute().parseAs(PlacesList.class);
        // Check log cat for places response status
        Log.d("Places Status", "" + list.status);
        return list;
    } catch (HttpResponseException e) {
        Log.e("Error:", e.getMessage());
        return null;
    }
}