1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
package tim.prune.function.search;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import tim.prune.App;
import tim.prune.I18nManager;
import tim.prune.data.DataPoint;
/**
* Function to load information about geocaches nearby to the current point
* using the service at opencaching.de
*/
public class SearchOpenCachingDeFunction extends GenericDownloaderFunction
{
/** Maximum distance from point in km */
private static final int MAX_DISTANCE = 50;
/**
* Constructor
* @param inApp App object
*/
public SearchOpenCachingDeFunction(App inApp) {
super(inApp);
}
/**
* @return name key
*/
public String getNameKey() {
return "function.searchopencachingde";
}
/**
* @param inColNum index of column, 0 or 1
* @return key for this column
*/
protected String getColumnKey(int inColNum)
{
if (inColNum == 0) return "dialog.wikipedia.column.name";
return "dialog.wikipedia.column.distance";
}
/**
* Run method to call service in a separate thread
*/
public void run()
{
_statusLabel.setText(I18nManager.getText("confirm.running"));
// Get coordinates from current point
DataPoint point = _app.getTrackInfo().getCurrentPoint();
if (point == null) {
return;
}
final double lat = point.getLatitude().getDouble();
final double lon = point.getLongitude().getDouble();
submitSearch(lat, lon);
// Set status label according to error or "none found", leave blank if ok
if (_errorMessage == null && _trackListModel.isEmpty()) {
_errorMessage = I18nManager.getText("dialog.geocaching.nonefound");
}
_statusLabel.setText(_errorMessage == null ? "" : _errorMessage);
finishedSearch();
}
/**
* Submit the search for the given parameters
* @param inLat latitude
* @param inLon longitude
*/
private void submitSearch(double inLat, double inLon)
{
// The only parameters are lat and long from the current point
String urlString = "https://opencaching.de/search.php?searchto=searchbydistance&showresult=1"
+ "&output=XML&sort=bydistance&lat=" + inLat
+ "&lon=" + inLon + "&distance=" + MAX_DISTANCE + "&unit=km";
// Parse the returned XML with a special handler
OpenCachingDeXmlHandler xmlHandler = new OpenCachingDeXmlHandler();
try
{
URL url = new URL(urlString);
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
try (InputStream inStream = url.openStream()) {
saxParser.parse(inStream, xmlHandler);
}
} catch (SAXException | IOException | ParserConfigurationException ignored) {
}
// Add track list to model
ArrayList<SearchResult> trackList = xmlHandler.getTrackList();
_trackListModel.addTracks(trackList);
}
}
|