我可以用下面的代码找到附近的加油站,但如何找到最近的加油站呢?
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + source.latitude + "," + source.longitude);
googlePlacesUrl.append("&radius=" + PROOXIMITY_RADIUS);
googlePlacesUrl.append("&types=" + "gas_station");
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);使用上面的代码,我正在创建url以获得最近的加油站。
public String read(String httpUrl){
String httpData = "";
InputStream stream = null;
HttpURLConnection urlConnection = null;
try{
URL url = new URL(httpUrl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
stream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buf = new StringBuffer();
String line = "";
while((line = reader.readLine()) != null){
buf.append(line);
}
httpData = buf.toString();
reader.close();
} catch (Exception e) {
Log.e("HttpRequestHandler" , e.getMessage());
} finally {
try {
stream.close();
urlConnection.disconnect();
} catch (Exception e){
Log.e("HttpRequestHandler" , e.getMessage());
}
}
return httpData;
}在此之后,我将解析响应。
但我想做的是我想找到最近的那个?我可以用纬度和经度来计算距离,但我想一定有更简单的方法吗?
有人能帮我吗?
发布于 2017-08-10 20:08:53
最直接的方法是在请求中使用rankby=distance参数。
rankby -指定列出结果的顺序。注意,如果指定了半径(在上述所需参数下描述),则不应包括曲柄。可能的价值是: 日珥(默认)。此选项根据结果的重要性对结果进行排序。排名将有利于指定区域内的突出位置。排名在谷歌索引中的排名、全球受欢迎程度以及其他因素都会影响到公司的知名度。 距离.此选项根据搜索结果与指定位置的距离,将搜索结果按升序排列。指定距离时,需要一个或多个关键字、名称或类型。
https://developers.google.com/places/web-service/search#PlaceSearchRequests
代码片段如下(删除radius并添加rankby)
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + source.latitude + "," + source.longitude);
googlePlacesUrl.append("&rankby=distance");
googlePlacesUrl.append("&type=" + "gas_station");
googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);您还可以删除很久以前就不推荐使用的sensor参数。注意,types参数无效,您应该根据文档使用type。
类型-将结果限制在与指定类型匹配的位置。只能指定一个类型(如果提供了多个类型,则忽略第一个条目之后的所有类型)
最近的加油站将是反应的第一个。
我希望这能帮到你!
https://stackoverflow.com/questions/45612505
复制相似问题