1

たった今、Google API Key を購入しました。Google Places を使用して正確な場所を見つけました。最も重要なことは、プログラムで場所を見つける必要があることです。つまり、アプリケーションを開くと、場所がテキストビューに表示されます。また、最寄りの場所を見つけたいです。

誰でも完全なサンプルコードで説明できますか(JavaスクリプトコードではなくJavaで)

4

1 に答える 1

2

Google Places API には次のコードを使用できます

ここでは空港を検索していますが、自分の場所を検索できます。ここでgetJsonResponse id を 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 メソッドを呼び出して、現在の場所と取得した場所の間の距離を見つけ、最も近い場所を取得したことを計算した後、

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 は現在の緯度と経度で、lat long は取得した緯度と経度です。

于 2013-01-10T07:14:23.510 に答える