Find nearest gas station using google maps v2 in android and want to display the route to it

0 votes
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
        googlePlacesUrl.append("location=" + source.latitude + "," + source.longitude);
        googlePlacesUrl.append("&radius=" + PROOXIMITY_RADIUS);
        googlePlacesUrl.append("&types=" + "gas_station");
        googlePlacesUrl.append("&sensor=true");
        googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);

Using the above code, I am creating the url to get the nearest gas stations.

public String read(String httpUrl){
        String httpData = "";
        InputStream stream = null;
        HttpURLConnection urlConnection = null;
        try{
            URL url = new URL(httpUrl);
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.connect();
            stream = urlConnection.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buf = new StringBuffer();
            String line = "";
            while((line = reader.readLine()) != null){
                buf.append(line);
            }
            httpData = buf.toString();
            reader.close();
        } catch (Exception e) {
            Log.e("HttpRequestHandler" , e.getMessage());
        } finally {
            try {
                stream.close();
                urlConnection.disconnect();
            } catch (Exception e){
                Log.e("HttpRequestHandler" , e.getMessage());
            }
        }

        return httpData;
    }

After this , I am parsing the response.

But what I want to do is I want to find the nearest one? I can calculate the distance using latitude and longitude but I think there must be some easier way?

Jun 9, 2022 in Others by polo
• 1,480 points
654 views

1 answer to this question.

0 votes

The code snippet is the following (you remove radius and add rankby)

StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("location=" + source.latitude + "," + source.longitude);
googlePlacesUrl.append("&rankby=distance");
googlePlacesUrl.append("&type=" + "gas_station");
googlePlacesUrl.append("&key=" + GOOGLE_API_KEY);
answered Jun 9, 2022 by nisha
• 2,210 points

Related Questions In Others

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

I want to make Excel read a value in Calc and copy it to my sheet in Excel

Here is the sample code that will allow ...READ MORE

answered Oct 27, 2022 in Others by narikkadan
• 63,640 points
506 views
0 votes
0 answers
0 votes
0 answers

Android Maps Library V2 zoom controls custom position

How can I arrange the built-in zoom ...READ MORE

Nov 22, 2022 in Android by Edureka
• 12,690 points
597 views
0 votes
1 answer

Android - How can I find location using just PINCODE?

1 You can find a location (latitude, longitude) ...READ MORE

answered Jun 1, 2022 in Others by nisha
• 2,210 points
1,157 views
0 votes
1 answer

How to get current location in google map android

package com.example.sandeep.googlemapsample; import android.content.pm.PackageManager; import android.location.Location; import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v4.app.ActivityCompat; import ...READ MORE

answered Jun 1, 2022 in Others by nisha
• 2,210 points
1,594 views
0 votes
1 answer

How to open the Google Play Store directly from my Android application?

To open Google Play AND ONLY Google ...READ MORE

answered Jun 6, 2022 in Others by nisha
• 2,210 points
1,956 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP