android - 在 android 中使用 google maps v2 查找最近的加油站,并希望显示到它的路线

标签 android google-maps google-places-api google-maps-android-api-2

我可以使用以下代码找到附近的加油站,但我怎样才能找到最近的加油站?

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

在此之后,我正在解析响应。

但是我想做的是找最近的吗?我可以使用纬度和经度计算距离,但我认为一定有更简单的方法吗?

有人可以帮我吗?

最佳答案

最直接的方法是在请求中使用 rankby=distance 参数。

rankby — Specifies the order in which results are listed. Note that rankby must not be included if radius (described under Required parameters above) is specified. Possible values are:

prominence (default). This option sorts results based on their importance. Ranking will favor prominent places within the specified area. Prominence can be affected by a place's ranking in Google's index, global popularity, and other factors.

distance. This option biases search results in ascending order by their distance from the specified location. When distance is specified, one or more of keyword, name, or type is required.

https://developers.google.com/places/web-service/search#PlaceSearchRequests

代码 fragment 如下(删除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

type — Restricts the results to places matching the specified type. Only one type may be specified (if more than one type is provided, all types following the first entry are ignored)

最近的加油站将排在第一位。

希望对您有所帮助!

关于android - 在 android 中使用 google maps v2 查找最近的加油站,并希望显示到它的路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45612505/

相关文章:

google-maps - 谷歌地图 View 顶部的图像按钮

javascript - Google map 进行调整以包含用户和地址

javascript - 谷歌地图标记点击事件未触发

java - 膨胀类 android.fragment.app.FragmentContainerView 时出错,有人可以告诉我我缺少什么吗

google-places-api - 验证地点所有者 Google Places API

android - 如何在android中通过包名获取应用程序名称?

android - 执行 assembleRelease Gradle 任务现在可以从自定义任务中执行

javascript - 机场的 Google Places 自动完成功能

android - 蜂窝操作栏和边距

android - 如何解压、编辑和压缩安卓 apk