aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/geocode/GoogleReverseGeocoder.java
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-05-07 15:39:42 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-05-07 15:39:42 +1200
commit953852e6e984f8a639cf1a5ac4116fff755d538a (patch)
tree1902beb226bfa2ba7b60b20a310b376e8e86ce76 /src/org/traccar/geocode/GoogleReverseGeocoder.java
parent6a07fc479021dbf61143d432285c573faf38d6b9 (diff)
downloadtrackermap-server-953852e6e984f8a639cf1a5ac4116fff755d538a.tar.gz
trackermap-server-953852e6e984f8a639cf1a5ac4116fff755d538a.tar.bz2
trackermap-server-953852e6e984f8a639cf1a5ac4116fff755d538a.zip
Add Gisgraphy reverse geocoder
Diffstat (limited to 'src/org/traccar/geocode/GoogleReverseGeocoder.java')
-rw-r--r--src/org/traccar/geocode/GoogleReverseGeocoder.java62
1 files changed, 41 insertions, 21 deletions
diff --git a/src/org/traccar/geocode/GoogleReverseGeocoder.java b/src/org/traccar/geocode/GoogleReverseGeocoder.java
index 4ec6e10af..dae9915d8 100644
--- a/src/org/traccar/geocode/GoogleReverseGeocoder.java
+++ b/src/org/traccar/geocode/GoogleReverseGeocoder.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2013 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,42 +15,62 @@
*/
package org.traccar.geocode;
-import java.io.BufferedReader;
-import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
-import java.nio.charset.Charset;
import org.traccar.helper.Log;
+import javax.json.*;
+
public class GoogleReverseGeocoder implements ReverseGeocoder {
- private final static String MARKER = "\"formatted_address\" : \"";
+ private final String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f";
@Override
- public String getAddress(double latitude, double longitude) {
+ public String getAddress(AddressFormat format, double latitude, double longitude) {
try {
- URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + latitude + "," + longitude + "&sensor=false");
- URLConnection connection = url.openConnection();
-
- connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
-
- BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),Charset.forName("UTF-8")));
-
- // Find address line
- String line;
- while ((line = reader.readLine()) != null) {
- int index = line.indexOf(MARKER);
- if (index != -1) {
- return line.substring(index + MARKER.length(), line.length() - 2);
+ Address address = new Address();
+ URLConnection conn = new URL(String.format(url, latitude, longitude)).openConnection();
+
+ JsonObject json = Json.createReader(new InputStreamReader(conn.getInputStream())).readObject();
+ JsonObject result = (JsonObject) json.getJsonArray("results").get(0);
+ JsonArray components = result.getJsonArray("address_components");
+
+ for (JsonObject component : components.getValuesAs(JsonObject.class)) {
+
+ String value = component.getString("short_name");
+
+ for (JsonString type : component.getJsonArray("types").getValuesAs(JsonString.class)) {
+ if (type.getString().equals("street_number")) {
+ address.setHouse(value);
+ break;
+ } else if (type.getString().equals("route")) {
+ address.setStreet(value);
+ break;
+ } else if (type.getString().equals("locality")) {
+ address.setSettlement(value);
+ break;
+ } else if (type.getString().equals("administrative_area_level_2")) {
+ address.setDistrict(value);
+ break;
+ } else if (type.getString().equals("administrative_area_level_1")) {
+ address.setState(value);
+ break;
+ } else if (type.getString().equals("country")) {
+ address.setCountry(value);
+ break;
+ } else if (type.getString().equals("postal_code")) {
+ address.setPostcode(value);
+ break;
+ }
}
}
- reader.close();
+ return format.format(address);
- } catch(IOException error) {
+ } catch(Exception error) {
Log.warning(error);
}