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?