13 |
import com.google.code.geocoder.model.GeocoderStatus; |
import com.google.code.geocoder.model.GeocoderStatus; |
14 |
import com.google.gson.Gson; |
import com.google.gson.Gson; |
15 |
|
|
16 |
|
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
17 |
import dk.daoas.daoadresseservice.beans.OSMAddress; |
import dk.daoas.daoadresseservice.beans.OSMAddress; |
18 |
import dk.daoas.daoadresseservice.util.HttpUtil; |
import dk.daoas.daoadresseservice.util.HttpUtil; |
19 |
|
|
20 |
public class GeocodeHelper { |
public class GeocodeHelper { |
21 |
|
|
|
|
|
|
|
|
22 |
|
|
23 |
|
|
24 |
public static void main(String[] args) throws IOException { |
public static void main(String[] args) throws IOException { |
25 |
|
ServiceConfig conf = new ServiceConfig(); |
26 |
|
|
27 |
int post = 8700; |
int post = 8700; |
28 |
String vej = "Enebarvej"; |
String vej = "Enebarvej"; |
29 |
|
|
30 |
long start1 = System.currentTimeMillis(); |
long start1 = System.currentTimeMillis(); |
31 |
System.out.println( "Google:" + GeocodeHelper.googleHelper(post, vej) ); |
System.out.println( "Google:" + GeocodeHelper.googleHelper(conf, post, vej) ); |
32 |
long stop1 = System.currentTimeMillis(); |
long stop1 = System.currentTimeMillis(); |
33 |
|
|
34 |
long start2 = System.currentTimeMillis(); |
long start2 = System.currentTimeMillis(); |
72 |
|
|
73 |
|
|
74 |
|
|
75 |
public static String googleHelper(int postnr, String vejnavn) { |
public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) { |
76 |
try { |
try { |
77 |
//Todo: Load api key from context config |
//Todo: Load api key from context config |
78 |
final Geocoder geocoder = new Geocoder(); |
final Geocoder geocoder; |
79 |
|
if ( conf.googleApiKey != null ) { |
80 |
|
geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException |
81 |
|
} else { |
82 |
|
geocoder = new Geocoder(); |
83 |
|
} |
84 |
|
|
85 |
|
|
86 |
String search = vejnavn + ", " + postnr + ", Denmark"; |
String search = vejnavn + ", " + postnr + ", Denmark"; |
87 |
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest(); |
GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest(); |
90 |
|
|
91 |
//System.out.println( "Status: >" + geocoderResponse.getStatus() + "<"); |
//System.out.println( "Status: >" + geocoderResponse.getStatus() + "<"); |
92 |
if ( geocoderResponse.getStatus() != GeocoderStatus.OK) { |
if ( geocoderResponse.getStatus() != GeocoderStatus.OK) { |
93 |
|
System.out.println("Google responded with " + geocoderResponse.getStatus() ); |
94 |
return null; |
return null; |
95 |
} |
} |
96 |
|
|
110 |
} |
} |
111 |
} |
} |
112 |
|
|
113 |
} catch (IOException e) { |
} catch (Exception e) { |
114 |
System.out.println( "GoogleError: " + e.getMessage() ); |
System.out.println( "GoogleError: " + e.getMessage() ); |
115 |
} |
} |
116 |
return null; |
return null; |