简体   繁体   中英

Using HTTP Get request to overcome the Geocoder limits in Android

Is it possible to use HTTP API and perform HTTP Get request for Google maps in order to overcome the limits of using Geocoder API when requesting latitude and longitude of places?

Something like-

       URL url = new URL("https://www.google.com/maps/place/Paris/");
       connection.setDoOutput(true);
       connection.setDoInput(true);
       connection.setRequestMethod("GET");
       System.out.println("Value" + connection.getResponseCode());
       System.out.println(connection.getResponseMessage());
       System.out.println("content"+connection.getContent());

or

        URL url = new URL("https://www.google.com/maps/place/Paris/");
        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
        String strTemp = "";
        while (null != (strTemp = br.readLine())) {
            System.out.println(strTemp);
        }

Expecting the response to contain the lat and long of the place as in Google maps site, that way my client appears as a regular web client of google maps.

The Places API request has quota limit too, you can see the detail in this page: https://developers.google.com/places/webservice/usage

Also, you need an API key to do your Places API request, a sample way to do a Places API URL request in Android should be like this:

                URL placeUrl = new URL("https://maps.googleapis.com/maps/api/place/textsearch/json?query=restaurants+in+Sydney&key=AddYourOwnKeyHere");
                HttpURLConnection connection = (HttpURLConnection)placeUrl.openConnection();
                connection.setRequestMethod("GET");
                connection.connect();


                responseCode = connection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {

                    BufferedReader reader = null;

                    InputStream inputStream = connection.getInputStream();
                    StringBuffer buffer = new StringBuffer();
                    if (inputStream == null) {
                        // Nothing to do.
                        return null;
                    }
                    reader = new BufferedReader(new InputStreamReader(inputStream));

                    String line;
                    while ((line = reader.readLine()) != null) {

                        buffer.append(line + "\n");
                    }

                    if (buffer.length() == 0) {
                        return null;
                    }

                    Log.d(TAG, buffer.toString());
                }
                else {
                    Log.i(TAG, "Unsuccessful HTTP Response Code: " + responseCode);
                }

You should do this URL request in background thread, for example, in the doInBackground() method of AsyncTask .

You can also visit this tutorial for more detail about how to use Places API in Android.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM