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

0 votes

The following code will help me locate neighboring gas stations, but how do I locate the one that is closest to me?

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;
    }

I am now parsing the response after this.

But my main objective is to locate the closest. Even though I can calculate the distance using latitude and longitude, there must be a simpler method.

Can somebody assist me in that?

Nov 25, 2022 in Android by Edureka
• 13,620 points
803 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Android

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

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

If you want to link to your ...READ MORE

answered Nov 7, 2022 in Android by Edureka
• 12,690 points
4,356 views
0 votes
1 answer

How to display Toast in Android?

Use the show() function of the Toast ...READ MORE

answered Nov 8, 2022 in Android by Edureka
• 12,690 points
1,444 views
0 votes
0 answers

How to keep marker on road? Google Maps Android

I'm working on an app that displays ...READ MORE

Nov 9, 2022 in Android by Edureka
• 12,690 points
378 views
0 votes
1 answer
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
607 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,187 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