Further refactorings (geocoder).
parent
979973e1e6
commit
ef91e42507
|
@ -20,4 +20,5 @@ public class AppConstants {
|
|||
static final int SETTINGS_ID_INTENT_REQUEST_ID = 2;
|
||||
static final String PHOTO_INFO = "PHOTO_INFO";
|
||||
static final String SAVED_CONFIG_KEY = "SAVED_CONFIG_KEY";
|
||||
public static final String GOOGLE_API_KEY = "AIzaSyBAJoK-pu_qnQ0U8EGjM1Zkz_g8oJV4w2g";
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.greenrobot.eventbus.Subscribe;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import pl.tpolgrabia.panoramiobindings.utils.PanoramioUtils;
|
||||
import pl.tpolgrabia.urbanexplorer.AppConstants;
|
||||
import pl.tpolgrabia.urbanexplorer.MainActivity;
|
||||
import pl.tpolgrabia.urbanexplorer.R;
|
||||
import pl.tpolgrabia.urbanexplorer.callbacks.*;
|
||||
|
@ -53,6 +54,7 @@ public class HomeFragment extends Fragment {
|
|||
private ArrayList<PanoramioImageInfo> photos;
|
||||
private boolean noMorePhotos;
|
||||
private String currentGeocodedLocation;
|
||||
private GeocoderUtils geocoderUtils;
|
||||
|
||||
public HomeFragment() {
|
||||
// Required empty public constructor
|
||||
|
@ -75,6 +77,7 @@ public class HomeFragment extends Fragment {
|
|||
final StandardLocationListener locationCallback = mainActivity.getLocationCallback();
|
||||
locationCallback.addCallback(new PanoramioLocationCallback(this));
|
||||
locationCallback.addProviderCallback(new PanoramioProviderCallback(this));
|
||||
geocoderUtils = new GeocoderUtils(getActivity(), AppConstants.GOOGLE_API_KEY);
|
||||
}
|
||||
|
||||
public void updateGeocodedLocation() {
|
||||
|
@ -90,7 +93,7 @@ public class HomeFragment extends Fragment {
|
|||
return;
|
||||
}
|
||||
|
||||
LocationUtils.getGeoCodedLocation(getActivity(),
|
||||
geocoderUtils.getGeoCodedLocation(
|
||||
currLocation.getLatitude(),
|
||||
currLocation.getLongitude(),
|
||||
new GeocodedLocationCallback(this));
|
||||
|
|
|
@ -15,6 +15,7 @@ import org.greenrobot.eventbus.EventBus;
|
|||
import org.greenrobot.eventbus.Subscribe;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import pl.tpolgrabia.urbanexplorer.AppConstants;
|
||||
import pl.tpolgrabia.urbanexplorer.MainActivity;
|
||||
import pl.tpolgrabia.urbanexplorer.R;
|
||||
import pl.tpolgrabia.urbanexplorer.adapters.WikiLocationsAdapter;
|
||||
|
@ -43,6 +44,7 @@ public class WikiLocationsFragment extends Fragment {
|
|||
private ArrayList<WikiAppObject> appObjects = new ArrayList<>();
|
||||
private int lastFetchSize = -1;
|
||||
private String currentGeocodedLocation;
|
||||
private GeocoderUtils geocoderUtils;
|
||||
|
||||
public WikiLocationsFragment() {
|
||||
// Required empty public constructor
|
||||
|
@ -59,6 +61,7 @@ public class WikiLocationsFragment extends Fragment {
|
|||
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
|
||||
super.onActivityCreated(savedInstanceState);
|
||||
appObjects = WikiCacheUtils.loadWikiObjectsFromCache(getActivity(), savedInstanceState);
|
||||
geocoderUtils = new GeocoderUtils(getActivity(), AppConstants.GOOGLE_API_KEY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -123,7 +126,7 @@ public class WikiLocationsFragment extends Fragment {
|
|||
}
|
||||
|
||||
private void updateGeocodedLocation() {
|
||||
WikiUtils.getGeoCodedLocation(getActivity(), new WikiLocationGeoCoderCallback(this));
|
||||
geocoderUtils.getGeoCodedLocation(new WikiLocationGeoCoderCallback(this));
|
||||
}
|
||||
|
||||
public void updateLocationInfo() {
|
||||
|
|
|
@ -0,0 +1,132 @@
|
|||
package pl.tpolgrabia.urbanexplorer.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.location.Location;
|
||||
import com.androidquery.AQuery;
|
||||
import com.androidquery.callback.AjaxCallback;
|
||||
import com.androidquery.callback.AjaxStatus;
|
||||
import org.json.JSONArray;
|
||||
import org.json.JSONObject;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import pl.tpolgrabia.urbanexplorerutils.callbacks.LocationGeoCoderCallback;
|
||||
import pl.tpolgrabia.urbanexplorerutils.constants.UtilConstants;
|
||||
import pl.tpolgrabia.urbanexplorerutils.utils.LocationUtils;
|
||||
import pl.tpolgrabia.wikibinding.utils.WikiUtils;
|
||||
|
||||
/**
|
||||
* Created by tpolgrabia on 26.09.16.
|
||||
*/
|
||||
public class GeocoderUtils {
|
||||
|
||||
private static final Logger lg = LoggerFactory.getLogger(GeocoderUtils.class);
|
||||
private Context ctx;
|
||||
private final String googleApiKey;
|
||||
|
||||
public GeocoderUtils(Context ctx, String googleApiKey) {
|
||||
this.ctx = ctx;
|
||||
this.googleApiKey = googleApiKey;
|
||||
}
|
||||
|
||||
public void getGeoCodedLocation(LocationGeoCoderCallback clbk) {
|
||||
|
||||
if (ctx == null) {
|
||||
lg.warn("Context is null - not available");
|
||||
clbk.callback(-1, "ERROR", "ERROR", "Not available");
|
||||
return;
|
||||
}
|
||||
|
||||
Location location = LocationUtils.getLastKnownLocation(ctx);
|
||||
|
||||
if (location == null) {
|
||||
lg.debug("Location is still not available");
|
||||
return;
|
||||
}
|
||||
|
||||
getGeoCodedLocation(
|
||||
location.getLatitude(),
|
||||
location.getLongitude(),
|
||||
clbk);
|
||||
}
|
||||
|
||||
public void getGeoCodedLocation(Double latitude, Double longitude,
|
||||
final LocationGeoCoderCallback clbk) {
|
||||
if (ctx == null) {
|
||||
throw new IllegalArgumentException("Context cannot be null");
|
||||
}
|
||||
|
||||
if (latitude == null) {
|
||||
throw new IllegalArgumentException("Latitude cannot be null");
|
||||
}
|
||||
|
||||
if (longitude == null) {
|
||||
throw new IllegalArgumentException("Longitude cannot be null");
|
||||
}
|
||||
|
||||
AQuery aq = new AQuery(ctx);
|
||||
|
||||
aq.ajax("https://maps.googleapis.com/maps/api/geocode/json" +
|
||||
"?latlng=" + latitude + "," + longitude +
|
||||
"&key=" + googleApiKey, JSONObject.class, new AjaxCallback<JSONObject>(){
|
||||
@Override
|
||||
public void callback(String url, JSONObject object, AjaxStatus status) {
|
||||
lg.debug("Got response from url {} with status {} - {}",
|
||||
url,
|
||||
status,
|
||||
object);
|
||||
|
||||
String googleStatus = object != null ? object.optString("status") : "(null)";
|
||||
lg.trace("Google status {}", googleStatus);
|
||||
|
||||
if (status.getCode() != 200) {
|
||||
lg.info("Got invalid response with error code {} and message {} and error {}",
|
||||
status.getCode(), status.getMessage(), status.getError());
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!"OK".equals(googleStatus)) {
|
||||
lg.info("Got invalid google status {}", googleStatus);
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, null);
|
||||
return;
|
||||
}
|
||||
|
||||
JSONArray results = object.optJSONArray("results");
|
||||
if (results == null) {
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, null);
|
||||
return;
|
||||
}
|
||||
|
||||
int n = results.length();
|
||||
for (int i = 0; i < n; i++) {
|
||||
result = results.optJSONObject(i);
|
||||
if (result == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
JSONArray types = result.optJSONArray("types");
|
||||
if (types == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (types.length() != 1){
|
||||
continue;
|
||||
}
|
||||
|
||||
String singleType = types.optString(0);
|
||||
if (!"street_address".equals(singleType)) {
|
||||
continue;
|
||||
}
|
||||
clbk.callback(status.getCode(),
|
||||
status.getMessage(),
|
||||
googleStatus,
|
||||
result.optString("formatted_address"));
|
||||
return;
|
||||
}
|
||||
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, "(not found)");
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
|
@ -43,87 +43,6 @@ public class LocationUtils {
|
|||
return null;
|
||||
}
|
||||
|
||||
public static void getGeoCodedLocation(Context ctx, Double latitude, Double longitude,
|
||||
final LocationGeoCoderCallback clbk) {
|
||||
if (ctx == null) {
|
||||
throw new IllegalArgumentException("Context cannot be null");
|
||||
}
|
||||
|
||||
if (latitude == null) {
|
||||
throw new IllegalArgumentException("Latitude cannot be null");
|
||||
}
|
||||
|
||||
if (longitude == null) {
|
||||
throw new IllegalArgumentException("Longitude cannot be null");
|
||||
}
|
||||
|
||||
AQuery aq = new AQuery(ctx);
|
||||
|
||||
aq.ajax("https://maps.googleapis.com/maps/api/geocode/json" +
|
||||
"?latlng=" + latitude + "," + longitude +
|
||||
"&key=" + UtilConstants.GOOGLE_API_KEY, JSONObject.class, new AjaxCallback<JSONObject>(){
|
||||
@Override
|
||||
public void callback(String url, JSONObject object, AjaxStatus status) {
|
||||
lg.debug("Got response from url {} with status {} - {}",
|
||||
url,
|
||||
status,
|
||||
object);
|
||||
|
||||
String googleStatus = object != null ? object.optString("status") : "(null)";
|
||||
lg.trace("Google status {}", googleStatus);
|
||||
|
||||
if (status.getCode() != 200) {
|
||||
lg.info("Got invalid response with error code {} and message {} and error {}",
|
||||
status.getCode(), status.getMessage(), status.getError());
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, null);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!"OK".equals(googleStatus)) {
|
||||
lg.info("Got invalid google status {}", googleStatus);
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, null);
|
||||
return;
|
||||
}
|
||||
|
||||
JSONArray results = object.optJSONArray("results");
|
||||
if (results == null) {
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, null);
|
||||
return;
|
||||
}
|
||||
|
||||
int n = results.length();
|
||||
for (int i = 0; i < n; i++) {
|
||||
result = results.optJSONObject(i);
|
||||
if (result == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
JSONArray types = result.optJSONArray("types");
|
||||
if (types == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (types.length() != 1){
|
||||
continue;
|
||||
}
|
||||
|
||||
String singleType = types.optString(0);
|
||||
if (!"street_address".equals(singleType)) {
|
||||
continue;
|
||||
}
|
||||
clbk.callback(status.getCode(),
|
||||
status.getMessage(),
|
||||
googleStatus,
|
||||
result.optString("formatted_address"));
|
||||
return;
|
||||
}
|
||||
|
||||
clbk.callback(status.getCode(), status.getMessage(), googleStatus, "(not found)");
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static Location getLastKnownLocation(Context ctx) {
|
||||
String locationProvider = getDefaultLocation(ctx);
|
||||
|
||||
|
|
|
@ -359,27 +359,6 @@ public class WikiUtils {
|
|||
);
|
||||
}
|
||||
|
||||
public static void getGeoCodedLocation(Context ctx, LocationGeoCoderCallback clbk) {
|
||||
|
||||
if (ctx == null) {
|
||||
lg.warn("Context is null - not available");
|
||||
clbk.callback(-1, "ERROR", "ERROR", "Not available");
|
||||
return;
|
||||
}
|
||||
|
||||
Location location = LocationUtils.getLastKnownLocation(ctx);
|
||||
|
||||
if (location == null) {
|
||||
lg.debug("Location is still not available");
|
||||
return;
|
||||
}
|
||||
|
||||
LocationUtils.getGeoCodedLocation(ctx,
|
||||
location.getLatitude(),
|
||||
location.getLongitude(),
|
||||
clbk);
|
||||
}
|
||||
|
||||
public static void fetchAppData(Context ctx, WikiAppResponseCallback clbk) {
|
||||
final Location location = LocationUtils.getLastKnownLocation(ctx);
|
||||
|
||||
|
|
Loading…
Reference in New Issue