package dk.daoas.fulddaekning.osrm; import java.io.IOException; import java.util.Collection; import java.util.Iterator; import com.google.gson.Gson; import dk.daoas.fulddaekning.Adresse; import dk.daoas.fulddaekning.HttpUtil; public class OSRMHelper { Gson gson = new Gson(); final String base_url = "http://10.30.2.61:5000"; public Adresse getNearestViaTable(Adresse a1, Collection haystack) { Adresse bedsteAdresse = null; Adresse hayArray[] = new Adresse[ haystack.size() ]; haystack.toArray(hayArray); StringBuilder sb = new StringBuilder(); sb.append(base_url); sb.append("/table?loc=").append(a1.latitude).append(",").append(a1.longitude); for(int i = 0; i haystack) { int bedsteAfstand = 9999999; Adresse bedsteAdresse = null; Iterator it = haystack.iterator(); while (it.hasNext()) { Adresse a2 = it.next(); try { OSRMResponse res = getRoute(a1, a2); if (res.route_summary.total_distance < bedsteAfstand) { bedsteAfstand = res.route_summary.total_distance; bedsteAdresse = a2; } } catch (IOException e) { System.out.println( e.getMessage() ); System.exit(1); } } return bedsteAdresse; } public OSRMResponse getRoute(Adresse a1, Adresse a2) throws IOException { String loc1 = a1.latitude + "," + a1.longitude; String loc2 = a2.latitude + "," + a2.longitude; String params = "loc=" + loc1 + "&loc=" + loc2 + "&geometry=false&instructions=false&alt=false"; String url = base_url + "/viaroute?" + params; String txtResponse = HttpUtil.getContentString(url, 500, "UTF-8"); return gson.fromJson(txtResponse, OSRMResponse.class); } }