近くのガソリンスタンドは次のコードを使用して検索できますが、どうすれば最も近いものを見つけることができますか?アンドロイドのGoogleマップv2を使用して最寄りのガソリンスタンドを検索し、そのルートを表示したい
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);
上記のコードを使用して、私は最も近いガソリンスタンドを取得するためのURLを作成しています。
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;
}
この後、私は応答を解析しています。
しかし、私が最も近いものを見つけるしたいのか、私がやりたいのですか?私は緯度と経度を使って距離を計算することができますが、もっと簡単な方法があると思いますか?
誰でも助けてくれますか?
ありがとうございましたxomena。それは本当に助けになりました。 –