-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored code to add new location and util files.
- Loading branch information
1 parent
98af3a4
commit 08dc682
Showing
4 changed files
with
74 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package map; | ||
|
||
public class Location { | ||
|
||
private double lat; | ||
private double lon; | ||
|
||
public Location(double lat, double lon) { | ||
this.setLat(lat); | ||
this.setLon(lon); | ||
} | ||
|
||
public double getLat() { | ||
return lat; | ||
} | ||
|
||
public void setLat(double lat) { | ||
this.lat = lat; | ||
} | ||
|
||
public double getLon() { | ||
return lon; | ||
} | ||
|
||
public void setLon(double lon) { | ||
this.lon = lon; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,16 @@ | ||
package map; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
|
||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
|
||
public class Lookup { | ||
public static String lookupPlace(String query) { | ||
JSONArray results = new JSONArray(); | ||
try { | ||
String urlString = "https://nominatim.openstreetmap.org/search?q=" + URLEncoder.encode(query, "UTF-8") + "&format=json"; | ||
URL url = new URL(urlString); | ||
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) { | ||
StringBuilder sb = new StringBuilder(); | ||
int cp; | ||
while ((cp = in.read()) != -1) { | ||
sb.append((char) cp); | ||
} | ||
in.close(); | ||
String content = sb.toString(); | ||
results = new JSONArray(content); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (UnsupportedEncodingException | MalformedURLException e) { | ||
e.printStackTrace(); | ||
} | ||
public static Location lookupPlace(String query) { | ||
JSONArray results = Util.queryURL(query); | ||
if (results.isEmpty()) return null; | ||
|
||
JSONObject best = results.getJSONObject(0); | ||
double lat = best.getDouble("lat"); | ||
double lon = best.getDouble("lon"); | ||
return lat + "\t" + lon; | ||
return new Location(lat, lon); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package map; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.UnsupportedEncodingException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLEncoder; | ||
|
||
import org.json.JSONArray; | ||
|
||
public class Util { | ||
|
||
static JSONArray queryURL(String query) { | ||
JSONArray results = new JSONArray(); | ||
try { | ||
String urlString = "https://nominatim.openstreetmap.org/search?q=" + URLEncoder.encode(query, "UTF-8") + "&format=json"; | ||
URL url = new URL(urlString); | ||
try (BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()))) { | ||
StringBuilder sb = new StringBuilder(); | ||
int cp; | ||
while ((cp = in.read()) != -1) { | ||
sb.append((char) cp); | ||
} | ||
in.close(); | ||
String content = sb.toString(); | ||
results = new JSONArray(content); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} catch (UnsupportedEncodingException | MalformedURLException e) { | ||
e.printStackTrace(); | ||
} | ||
return results; | ||
} | ||
|
||
} |