Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 40 additions & 15 deletions src/main/java/de/meggsimum/w3w/What3Words.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* A Java wrapper for the What3Words Web-API.
*
*
* @see http://developer.what3words.com/api/
*
* @author Christian Mayer, meggsimum
Expand Down Expand Up @@ -50,28 +50,40 @@ public What3Words(String apiKey) {
* given language.
*
* @param apiKey
* @param language
* @param language default Language
*/
public What3Words(String apiKey, String language) {
this.apiKey = apiKey;
this.language = language;
}

/**
* Converts 3 words into a position.
* Converts 3 words into a position specifying default language.
*
* @param words
* the 3 words representing the "w3w-address"
* @return array holding the position in the form [lat, lon]
* @throws IOException
* @throws What3WordsException
* @throws What3WordsException
*/
public double[] wordsToPosition(String[] words) throws IOException,
What3WordsException {
return wordsToPosition(words, this.language);
}

String url = this.baseUrl + "w3w?key=" + this.apiKey + "&string="
+ words[0] + "." + words[1] + "." + words[2] + "&lang="
+ this.language;
/**
* Converts 3 words into a position.
*
* @param words
* the 3 words representing the "w3w-address"
* @param language
* @return array holding the position in the form [lat, lon]
* @throws IOException
* @throws What3WordsException
*/
public double[] wordsToPosition(String[] words, String language) throws IOException,
What3WordsException {
String url = String.format("%sw3w?key=%s&string=%s.%s.%s&lang=%s" ,this.baseUrl, this.apiKey, words[0], words[1], words[2], language);

String response = this.doHttpGet(url);

Expand All @@ -88,7 +100,7 @@ public double[] wordsToPosition(String[] words) throws IOException,

} else if (json.has("error")) {

throw new Exception("Error returned from w3w API: "
throw new What3WordsException("Error returned from w3w API: "
+ json.getString("message"));

} else {
Expand All @@ -103,20 +115,33 @@ public double[] wordsToPosition(String[] words) throws IOException,
}

/**
* Converts a position into a "w3w-address".
* Converts a position into a "w3w-address" with default language.
*
* @param coords
* Coordinates to be transformed in the form [lat, lon]
* @return Array holding the "w3w-address" in the form [word1, word2, word3]
* @throws IOException
* @throws What3WordsException
* @throws What3WordsException
*/
public String[] positionToWords(double[] coords)
throws What3WordsException, IOException {
return positionToWords(coords, this.language);
}

String url = this.baseUrl + "position?key=" + this.apiKey
+ "&position=" + coords[0] + "," + coords[1] + "&lang="
+ this.language;
/**
* Converts a position into a "w3w-address".
*
* @param coords
* Coordinates to be transformed in the form [lat, lon]
* @param language
* Language for the "w3w-address"
* @return Array holding the "w3w-address" in the form [word1, word2, word3]
* @throws IOException
* @throws What3WordsException
*/
public String[] positionToWords(double[] coords, String language)
throws What3WordsException, IOException {
String url = String.format("%sposition?key=%s&position=%s,%s&lang=%s" ,this.baseUrl, this.apiKey, coords[0], coords[1], language);

String response = this.doHttpGet(url);

Expand All @@ -135,7 +160,7 @@ public String[] positionToWords(double[] coords)

} else if (json.has("error")) {

throw new Exception("Error returned from w3w API: "
throw new What3WordsException("Error returned from w3w API: "
+ json.getString("message"));

} else {
Expand Down Expand Up @@ -180,7 +205,7 @@ private String doHttpGet(String url) throws IOException {
* @return the language
*/
public String getLanguage() {
return language;
return this.language;
}

/**
Expand Down