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?