--- dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java 2015/02/15 14:17:03 2300 +++ dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java 2015/02/15 16:09:11 2301 @@ -1,6 +1,7 @@ package dk.daoas.daoadresseservice; import java.io.IOException; +import java.net.URLEncoder; import java.util.List; import com.google.code.geocoder.Geocoder; @@ -10,16 +11,55 @@ import com.google.code.geocoder.model.GeocoderRequest; import com.google.code.geocoder.model.GeocoderResult; import com.google.code.geocoder.model.GeocoderStatus; +import com.google.gson.Gson; + +import dk.daoas.daoadresseservice.beans.OSMAddress; public class GeocodeHelper { + public static void main(String[] args) throws IOException { - System.out.println( GeocodeHelper.googleHelper(2800, "Chr. Xs Alle") ); + //System.out.println( GeocodeHelper.googleHelper(2800, "Chr. Xs Alle") ); + System.out.println( GeocodeHelper.openstreetmapHelper(2800, "Chr. Xs Alle") ); + System.out.println( GeocodeHelper.openstreetmapHelper(8700, "Enebaervej") ); + } + + public static String openstreetmapHelper(int postnr, String vejnavn) { + + try { + + String encVej = URLEncoder.encode(vejnavn, "UTF-8"); + + String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1"; + //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1"; + //System.out.println (url); + + String json = HttpUtil.getContentString(url, 1000); + //System.out.println(json); + + Gson gson = new Gson(); + OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class); + + //System.out.println("Count: " + adrList.length); + if (adrList.length != 1) + return null; + + if (adrList[0].address != null) { + return adrList[0].address.road; + } + + + } catch (Exception e) { + System.out.println( "Error: " + e.getMessage() ); + } + return null; } + + public static String googleHelper(int postnr, String vejnavn) { try { //Todo: Load api key from context config