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 in Others by polo
• 1,500 points
41 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 by nisha
• 2,160 points

Related Questions In Others

0 votes
2 answers
0 votes
0 answers
0 votes
2 answers

How to get the URL of the current tab in Google Chrome?

Its so simple.... If you want to ...READ MORE

answered Aug 12, 2020 in Others by Steve
• 200 points
701 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 in Others by nisha
• 2,160 points
30 views
0 votes
1 answer

Launching Google Maps Directions via an intent on Android

We can open Google Maps app using ...READ MORE

answered Jun 1 in Others by nisha
• 2,160 points
23 views
0 votes
1 answer

Running docker on Android

According to the documentation, the Android kernel is ...READ MORE

answered Aug 1, 2018 in Docker by Kalgi
• 52,350 points
2,952 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 in Others by nisha
• 2,160 points
17 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 in Others by nisha
• 2,160 points
16 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP