2222import java .util .ArrayList ;
2323import java .util .List ;
2424import java .util .Locale ;
25+ import java .util .Objects ;
2526import java .util .concurrent .atomic .AtomicBoolean ;
2627
2728import javax .net .ssl .HttpsURLConnection ;
@@ -33,12 +34,12 @@ public class BackendService extends GeocoderBackendService {
3334 private static final String SERVICE_URL_OSM = "https://nominatim.openstreetmap.org/" ;
3435
3536 private static final String REVERSE_GEOCODE_URL =
36- "%sreverse?format =json&key=%s &accept-language=%s&lat=%f&lon=%f" ;
37+ "%s/reverse?%sformat =json&accept-language=%s&lat=%f&lon=%f" ;
3738 private static final String SEARCH_GEOCODE_URL =
38- "%ssearch?format =json&key=%s &accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" ;
39+ "%s/search?%sformat =json&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" ;
3940 private static final String SEARCH_GEOCODE_WITH_BOX_URL =
40- "%ssearch?format=json&key=%s&accept-language=%s&addressdetails=1&bounded=1&q=%s&limit=%d" +
41- "&viewbox=%f,%f,%f,%f" ;
41+ SEARCH_GEOCODE_URL + "&viewbox=%f,%f,%f,%f" ;
42+
4243 private static final String WIRE_LATITUDE = "lat" ;
4344 private static final String WIRE_LONGITUDE = "lon" ;
4445 private static final String WIRE_ADDRESS = "address" ;
@@ -53,15 +54,31 @@ public class BackendService extends GeocoderBackendService {
5354 private static final String WIRE_COUNTRYNAME = "country" ;
5455 private static final String WIRE_COUNTRYCODE = "country_code" ;
5556
57+ private String mApiUrl ;
58+ private String mAPIKey ;
59+
60+ private void readPrefs () {
61+ SharedPreferences sp = PreferenceManager .getDefaultSharedPreferences (getApplicationContext ());
62+
63+ if (sp .getString (SettingsActivity .PrefsFragment .apiChoiceToken , "OSM" ).equals ("OSM" )) {
64+ mApiUrl = SERVICE_URL_OSM ;
65+ // No API key for OSM
66+ mAPIKey = "" ;
67+ } else {
68+ mApiUrl = SERVICE_URL_MAPQUEST ;
69+ mAPIKey = "key=" + sp .getString (SettingsActivity .PrefsFragment .mapQuestApiKeyToken , "NA" )
70+ + "&" ;
71+ }
72+ }
73+
74+
5675 @ Override
5776 protected List <Address > getFromLocation (double latitude , double longitude , int maxResults ,
5877 String locale ) {
78+ readPrefs ();
5979
60- SharedPreferences SP = PreferenceManager .getDefaultSharedPreferences (getBaseContext ());
61- String mapquestApiKey = SP .getString ("api_preference" , "NA" );
62-
63- String url = String .format (Locale .US , REVERSE_GEOCODE_URL , SERVICE_URL_MAPQUEST ,
64- mapquestApiKey , locale .split ("_" )[0 ], latitude , longitude );
80+ String url = String .format (Locale .US , REVERSE_GEOCODE_URL , mApiUrl , mAPIKey ,
81+ locale .split ("_" )[0 ], latitude , longitude );
6582 try {
6683 JSONObject result = new JSONObject (new AsyncGetRequest (this ,
6784 url ).asyncStart ().retrieveString ());
@@ -93,19 +110,17 @@ private static Locale localeFromLocaleString(String localeString) {
93110 protected List <Address > getFromLocationName (String locationName , int maxResults ,
94111 double lowerLeftLatitude , double lowerLeftLongitude , double upperRightLatitude ,
95112 double upperRightLongitude , String locale ) {
96-
97- SharedPreferences SP = PreferenceManager .getDefaultSharedPreferences (getBaseContext ());
98- String mapquestApiKey = SP .getString ("api_preference" , "NA" );
113+ readPrefs ();
99114
100115 String query = Uri .encode (locationName );
101116 String url ;
102117 if (lowerLeftLatitude == 0 && lowerLeftLongitude == 0 && upperRightLatitude == 0 &&
103118 upperRightLongitude == 0 ) {
104- url = String .format (Locale .US , SEARCH_GEOCODE_URL , SERVICE_URL_MAPQUEST ,
105- mapquestApiKey , locale .split ("_" )[0 ], query , maxResults );
119+ url = String .format (Locale .US , SEARCH_GEOCODE_URL , mApiUrl , mAPIKey ,
120+ locale .split ("_" )[0 ], query , maxResults );
106121 } else {
107- url = String .format (Locale .US , SEARCH_GEOCODE_WITH_BOX_URL , SERVICE_URL_MAPQUEST ,
108- mapquestApiKey , locale .split ("_" )[0 ], query , maxResults , lowerLeftLongitude ,
122+ url = String .format (Locale .US , SEARCH_GEOCODE_WITH_BOX_URL , mApiUrl , mAPIKey ,
123+ locale .split ("_" )[0 ], query , maxResults , lowerLeftLongitude ,
109124 upperRightLatitude , upperRightLongitude , lowerLeftLatitude );
110125 }
111126 try {
0 commit comments