何做我使用谷歌的地方找到在android系统的地方 [英] Ho do i use google places to find a place in android

查看:172
本文介绍了何做我使用谷歌的地方找到在android系统的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

刚才我已经购买谷歌API密钥。我已经找到使用谷歌的地方的确切地点。最重要的是我已经以编程方式发现的地方,这意味着当我打开我的应用程序会显示一个TextView的地方。此外,我想找到最近的地方。

Just now I've purchased Google API Key. I've find the exact place by using google Places. The most important thing is I've to find the place programmatically which means that When I open my application it will display the place in a textview. Also i want to find the nearest places.

任何人都可以请一个完整的示例code解释(在Java中,没有Java脚本code)

Can anyone please explain with a complete sample code (in java, not java script code)

推荐答案

您可以使用以下code对谷歌Places API的

You can use following code for Google places Api

在这里,我有搜索,但机场可以搜索你自己的地方,当你发现
这里的 getJsonResponse ID您的HttpClient

Here i have search airports but you can search your own places and when you find here getJsonResponse id your httpclient

String url = "https://maps.googleapis.com/maps/api/place/textsearch/json?query="**Place To Search**"&sensor=true&key=**Your Api Key**";
                String res = getJsonRespose(url);

                if (res != null && !res.equalsIgnoreCase("")) {
                    JSONObject jObject = new JSONObject(res);
                    // if (jObject.getString("status").equalsIgnoreCase("ok")) {
                    JSONArray jArray = jObject.getJSONArray("results");

                    if (jArray.length() > 0) {
                        for (int i = 0; i < jArray.length(); i++) {
                            AirportListData adata = new AirportListData();
                            JSONObject geo = jArray.getJSONObject(i);

                            JSONObject jLocation = geo.getJSONObject("geometry");
                            JSONObject jgetLocation = jLocation
                                    .getJSONObject("location");

                            Address = geo.getString("formatted_address");
                            name = geo.getString("name");
                            adata.setAirportName(name);
                            lat = jgetLocation.getDouble("lat");
                            lng = jgetLocation.getDouble("lng");
                            adata.setLat(lat);
                            adata.setLng(lng);
                            double dis = getDistance(lat, lng);
                            adata.setAddress(Address);
                            adata.setDistnace(dis / 1000);

                            ardata.add(adata);

                            Log.e("address", "Distance" + (dis / 1000) + "Address"
                                    + Address + "name" + name + "lat" + lat + "lng"
                                    + lng);
                        }

                    }

                }

当你得到了所有的地方,你可以打电话getDistance的方法,找到你的当前位置和你有地方之间计算,你有最近的地方距离后

When You got All places you can Call getDistance Method and find distance between your current place and places you got and after calculating that you got nearest place

public static double getDistance(double lat, double lng) {

        try {
            float[] result = new float[3];
            // Log.e("lat long : ", c.getDouble(1) + " : " + c.getDouble(2) +
            // " : " + latitude + " : " + longitude);
            Location.distanceBetween(Constantdata.lat, Constantdata.lon, lat,
                    lng, result);
            double distance = ((double) result[0]);
            return distance;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return 0.0;

    }

下面Constantdata.lat和constant.lon是您当前的纬度和经度和纬度长的地方纬度和logitude,你有

Here Constantdata.lat and constant.lon is your current latitude and longitude and lat long is places latitude and logitude that you got

这篇关于何做我使用谷歌的地方找到在android系统的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆