2013-03-27 2 views
0

Как использовать rankBy = расстояние для сортировки списка мест по расстоянию и отображение этого расстояния рядом с названием компании в списке? ???google places api rank по расстоянию

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

Вы можете использовать эту систему поиска URL, чтобы добиться того, что вы описали:

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

В настоящее время я работаю над этим.

  1. Не включайте параметр «radius» с rankBy = distance, как указано в документации API Google Places.
  2. Как только вы получите список мест назад, вам нужно будет сделать еще один звонок, чтобы получить информацию о каждом месте, чтобы вы могли получить адрес & Lat/Lng для каждого из них.
  3. Затем, используя текущее местоположение, вычислите расстояние от каждого места в результатах.
+0

как я вычислить его @Eldon Elledge ??? у вас есть примеры, которые я могу увидеть? – PropK

+0

любые примеры? @EldonElledge – PropK

+0

Пункт 1 балла полезен. –

Смежные вопросы