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

2019-08-08 18:27发布

问题:

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