Ho do i use google places to find a place in android

时光总嘲笑我的痴心妄想 提交于 2020-01-16 01:00:12

问题


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.

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


回答1:


You can use following code for Google places Api

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

                    }

                }

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;

    }

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



来源:https://stackoverflow.com/questions/14252476/ho-do-i-use-google-places-to-find-a-place-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!