谷歌放置api添加约束

时间:2014-11-18 13:01:56

标签: android google-places-api

链接“http://maps.googleapis.com/maps/api/geocode/json?latlng=%1 $ s,%2 $ s& sensor = true& language =”是从街道和邻居信息中排除的任何参数 我尝试添加类型(城市),但它不起作用

1 个答案:

答案 0 :(得分:0)

尝试这种方式:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + ","+ longitude + "&sensor=true");

完整代码:

class AsyGet extends AsyncTask<LatLng, Void, String> {
    ProgressDialog dialog;

    public AsyGet() {
        // TODO Auto-generated constructor stub

    }

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();

        dialog = new ProgressDialog(MapActivity.this);
        dialog.setMessage("Fetching location wait...");
        dialog.setCancelable(true);
        dialog.show();
    }

    @Override
    protected String doInBackground(LatLng... params) {
        // TODO Auto-generated method stub
        String responseBody = null;
        double latitude = params[0].latitude;
        double longitude = params[0].longitude;

        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + ","
                        + longitude + "&sensor=true"
        );
        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(0);


            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            // HttpResponse response = httpclient.execute(httppost);

            // Execute HTTP Post Request
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            responseBody = httpclient.execute(httppost, responseHandler);

            // Just display the response back
            // displayToastMessage(responseBody);

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return responseBody;

    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        //pd.dismiss();
        if (result != null) {

            if (result.length() == 0) {
                addressMap = "";
                Toast.makeText(getApplicationContext(), "Unable to retrieve profile information", Toast.LENGTH_SHORT).show();
            } else {


                JSONObject j;
                try {
                    j = new JSONObject(result);
                    String err = j.getString("status");


                    if (err.equalsIgnoreCase("OK")) {
                        JSONArray Results = j.getJSONArray("results");
                        JSONObject zero = Results.getJSONObject(0);
                        String full_address = zero.getString("formatted_address");

                        addressMap = full_address;

                        markerOptions.title(full_address);
                        CommonUtils.showToast(MapActivity.this, full_address);

                        // Placing a marker on the touched position
                        googleMap.addMarker(markerOptions);

                        dialog.dismiss();
                    }


                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


            }

        }

    }

}