Skip to content
This repository has been archived by the owner on May 27, 2024. It is now read-only.

Commit

Permalink
Merge pull request #367 from vreixo/issue363
Browse files Browse the repository at this point in the history
Fix #363 - Gathered alternative providers results for reverse geocoding ...
  • Loading branch information
barbeau committed Aug 27, 2014
2 parents 9aa2f70 + 2fb4633 commit 22b88d7
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public class OTPApp extends Application {
public static final int ADDRESS_MAX_LINES_TO_SHOW = 5;

//in meters
public static final int MARKER_GEOCODING_MAX_ERROR = 100;
public static final int GEOCODING_MAX_ERROR = 100;

public static final int HTTP_CONNECTION_TIMEOUT = 15000;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import android.location.Location;
import android.location.LocationManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
Expand Down Expand Up @@ -2393,7 +2392,7 @@ private void updateMarkerPosition(LatLng newLatLng, boolean isStartMarker) {
} else {
mIsEndLocationChangedByUser = false;
}
processAddress(isStartMarker, locationText, true);
processAddress(isStartMarker, locationText, newLatLng.latitude, newLatLng.longitude, true);
}

@Override
Expand Down Expand Up @@ -2522,7 +2521,23 @@ public void onSaveInstanceState(Bundle bundle) {
* shown.
*/
public void processAddress(final boolean isStartTextBox, String address,
boolean geocodingForMarker) {
boolean geocodingForMarker) {
processAddress(isStartTextBox, address, null, null, geocodingForMarker);
}

/**
* Triggers geocoding for chosen text box with passed text, offering the possibility of pass
* original latitude and longitude requested to check reverse geocoding results when
* geocoding for marker.
* <p>
* If address contents are the String used to identify user's location
* ("MyLocation" for example) user location is passed to know the
* corresponding address.
* In this case user's location shouldn't be null, if it is a toast is
* shown.
*/
public void processAddress(final boolean isStartTextBox, String address, Double originalLat,
Double originalLon, boolean geocodingForMarker) {
WeakReference<Activity> weakContext = new WeakReference<Activity>(getActivity());

mGeoCodingTask = new OTPGeocoding(weakContext, mApplicationContext,
Expand Down Expand Up @@ -2550,11 +2565,11 @@ public void processAddress(final boolean isStartTextBox, String address,
} else {
if (isStartTextBox){
mIsStartLocationGeocodingCompleted = false;
mGeoCodingTask.execute(address);
mGeoCodingTask.execute(address, originalLat.toString(), originalLon.toString());
}
else{
mIsEndLocationGeocodingCompleted = false;
mGeoCodingTask.execute(address);
mGeoCodingTask.execute(address, originalLat.toString(), originalLon.toString());
}
}
}
Expand Down Expand Up @@ -2817,7 +2832,7 @@ public void moveMarkerRelative(Boolean isStartMarker, CustomAddress address) {
Location.distanceBetween(marker.getPosition().latitude, marker.getPosition().longitude,
addressLat, addressLon, results);

if (results[0] < OTPApp.MARKER_GEOCODING_MAX_ERROR) {
if (results[0] < OTPApp.GEOCODING_MAX_ERROR) {
LatLng newLatlng = new LatLng(addressLat, addressLon);
setMarkerPosition(isStartMarker, newLatlng);
setTextBoxLocation(address.toString(), isStartMarker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected void onPreExecute() {

protected Long doInBackground(String... reqs) {
long count = reqs.length;
addressesReturn = LocationUtil.processGeocoding(context, selectedServer, reqs);
addressesReturn = LocationUtil.processGeocoding(context, selectedServer, geocodingForMarker, reqs);
return count;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,27 +147,29 @@ public static boolean checkPointInBoundingBox(LatLng location, Server selectedSe
return true;
}

public static ArrayList<CustomAddress> processGeocoding(Context context, Server selectedServer,
String... reqs) {
return processGeocoding(context, selectedServer, false, reqs);
}

public static ArrayList<CustomAddress> processGeocoding(Context context, Server selectedServer, String... reqs) {
public static ArrayList<CustomAddress> processGeocoding(Context context, Server selectedServer, boolean geocodingForMarker, String... reqs) {
ArrayList<CustomAddress> addressesReturn = new ArrayList<CustomAddress>();

String address = reqs[0];
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

if (address == null || address.equalsIgnoreCase("") || selectedServer == null) {
if (address == null || address.equalsIgnoreCase("")) {
return null;
}

if (address.equalsIgnoreCase(context.getString(R.string.text_box_my_location))) {
if (reqs.length >= 3){
String currentLat = reqs[1];
String currentLng = reqs[2];
LatLng latLng = new LatLng(Double.parseDouble(currentLat),
Double.parseDouble(currentLng));
double currentLat = Double.parseDouble(reqs[1]);
double currentLon = Double.parseDouble(reqs[2]);

CustomAddress addressReturn = new CustomAddress(context.getResources().getConfiguration().locale);
addressReturn.setLatitude(latLng.latitude);
addressReturn.setLongitude(latLng.longitude);
addressReturn.setLatitude(currentLat);
addressReturn.setLongitude(currentLon);
addressReturn.setAddressLine(addressReturn.getMaxAddressLineIndex() + 1,
context.getString(R.string.text_box_my_location));

Expand Down Expand Up @@ -205,8 +207,29 @@ public static ArrayList<CustomAddress> processGeocoding(Context context, Server

addresses = filterAddressesBBox(selectedServer, addresses);

if ((addresses == null) || addresses.isEmpty()) {
addresses = searchPlaces(context, selectedServer, address);
boolean resultsCloseEnough = true;

if (geocodingForMarker && reqs.length >= 3){
float results[] = new float[1];
resultsCloseEnough = false;
double originalLat = Double.parseDouble(reqs[1]);
double originalLon = Double.parseDouble(reqs[2]);

for (CustomAddress addressToCheck : addresses){
Location.distanceBetween(originalLat, originalLon,
addressToCheck.getLatitude(), addressToCheck.getLongitude(), results);
if (results[0] < OTPApp.GEOCODING_MAX_ERROR) {
resultsCloseEnough = true;
break;
}
}
}

if ((addresses == null) || addresses.isEmpty() || !resultsCloseEnough) {
if (addresses == null){
addresses = new ArrayList<CustomAddress>();
}
addresses.addAll(searchPlaces(context, selectedServer, address));

for (CustomAddress addressRetrieved : addresses) {
String str = addressRetrieved.getAddressLine(0);
Expand All @@ -219,7 +242,26 @@ public static ArrayList<CustomAddress> processGeocoding(Context context, Server

addresses = filterAddressesBBox(selectedServer, addresses);

addressesReturn.addAll(addresses);
if (geocodingForMarker && reqs.length >= 3 && addresses != null){
float results[] = new float[1];
double originalLat = Double.parseDouble(reqs[1]);
double originalLon = Double.parseDouble(reqs[2]);
float minDistanceToOriginalLatLon = Float.MAX_VALUE;
CustomAddress closestAddress = addresses.get(0);

for (CustomAddress addressToCheck : addresses){
Location.distanceBetween(originalLat, originalLon,
addressToCheck.getLatitude(), addressToCheck.getLongitude(), results);
if (results[0] < minDistanceToOriginalLatLon){
closestAddress = addressToCheck;
minDistanceToOriginalLatLon = results[0];
}
}
addressesReturn.add(closestAddress);
}
else{
addressesReturn.addAll(addresses);
}

return addressesReturn;
}
Expand All @@ -232,7 +274,7 @@ public static ArrayList<CustomAddress> processGeocoding(Context context, Server
* @return a new list filtered
*/
private static List<CustomAddress> filterAddressesBBox(Server selectedServer, List<CustomAddress> addresses) {
if (!(addresses == null || addresses.isEmpty())) {
if ((!(addresses == null || addresses.isEmpty())) && selectedServer != null) {
CopyOnWriteArrayList<CustomAddress> addressesFiltered = new CopyOnWriteArrayList<CustomAddress>(addresses);

for (CustomAddress address : addressesFiltered) {
Expand Down

0 comments on commit 22b88d7

Please sign in to comment.