Implement OpenStreetMap API call to retrieve the GPS location of any adress

This commit is contained in:
Lucas 2024-04-15 14:15:58 +02:00
parent f528d9f0c0
commit 10b3de6068

View file

@ -0,0 +1,54 @@
package fr.u_paris.gla.project.utils;
import fr.u_paris.gla.project.idfm.IDFMNetworkExtractor;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
public class ApiUtils {
/** The logger for information on the process */
private static final Logger LOGGER = Logger
.getLogger(IDFMNetworkExtractor.class.getName());
// OpenStreetMap API URL
private static final String OSM_URL = "https://nominatim.openstreetmap.org/search";
public static double[] getGPSLocation(String term) {
try {
String urlString = String.format("%s?q=%s&format=json", OSM_URL, URLEncoder.encode(term, StandardCharsets.UTF_8));
HttpURLConnection connection = (HttpURLConnection) new URL(urlString).openConnection();
connection.setRequestMethod("GET");
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
JSONArray jsonArray = new JSONArray(response.toString());
if (!jsonArray.isEmpty()) {
JSONObject firstResult = jsonArray.getJSONObject(0);
double lat = firstResult.getDouble("lat");
double lon = firstResult.getDouble("lon");
return new double[]{lat, lon};
}
}
catch (IOException e) {
LOGGER.log(Level.SEVERE, e,
() -> "Error accessing the API");
}
return new double[]{0, 0};
}
}