-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressFinder.java
276 lines (228 loc) · 8.71 KB
/
AddressFinder.java
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package in.androidfluid.locationsearch.util;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.io.Serializable;
import in.androidfluid.locationsearch.model.Address;
/**
* Created by nandan on 4/5/17.
*/
public abstract class AddressFinder extends AsyncTask<Void, Void, Address> {
private static final String TAG = AddressFinder.class.getSimpleName();
private static Address mAddress = new Address();
private String address = null;
private JSONObject jsonObj;
private String address1 = "";
private double latitude, longitude;
public AddressFinder(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public AddressFinder(String address) {
this.address = address;
}
private void setAddress() {
try {
String Status = jsonObj.getString("status");
if (Status.equalsIgnoreCase("OK")) {
JSONArray Results = jsonObj.getJSONArray("results");
JSONObject zero = Results.getJSONObject(0);
JSONArray address_components = zero.getJSONArray("address_components");
Log.d(TAG, "AddressArray: " + address_components.toString());
for (int i = 0; i < address_components.length(); i++) {
JSONObject zero2 = address_components.getJSONObject(i);
String long_name = zero2.getString("long_name");
JSONArray mtypes = zero2.getJSONArray("types");
String Type = mtypes.getString(0);
if (!TextUtils.isEmpty(long_name) || !long_name.equals("null") || long_name.length() > 0 || !long_name.equals("")) {
if (Type.equalsIgnoreCase("street_number")) {
mAddress.setAddress1(long_name + " ");
} else if (Type.equalsIgnoreCase("route")) {
mAddress.setAddress1(mAddress.getAddress1() + long_name);
Log.d(TAG, "address1 of setAddress: " + mAddress.getAddress1());
address1 = address1 + long_name;
} else if (Type.equalsIgnoreCase("sublocality")) {
mAddress.setAddress2(long_name);
Log.d(TAG, "Address2 of setAddress: " + mAddress.getAddress2());
//String address2 = long_name;
} else if (Type.equalsIgnoreCase("locality")) {
mAddress.setCity(long_name);
Log.d(TAG, "City of setAddress: " + mAddress.getCity());
//String city = long_name;
} else if (Type.equalsIgnoreCase("administrative_area_level_2")) {
mAddress.setCounty(long_name);
Log.d(TAG, "County of setAddress: " + mAddress.getCounty());
//String county = long_name;
} else if (Type.equalsIgnoreCase("administrative_area_level_1")) {
mAddress.setState(long_name);
Log.d(TAG, "State of setAddress: " + mAddress.getState());
//String state = long_name;
} else if (Type.equalsIgnoreCase("country")) {
mAddress.setCountry(long_name);
Log.d(TAG, "Country of setAddress: " + mAddress.getCountry());
//String country = long_name;
} else if (Type.equalsIgnoreCase("postal_code")) {
mAddress.setPIN(long_name);
Log.d(TAG, "Pin of setAddress: " + mAddress.getPIN());
//String PIN = long_name;
} else if (Type.equalsIgnoreCase("neighborhood")) {
mAddress.setArea(long_name);
//String area = long_name;
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void getGeoPoint() {
try {
longitude = ((JSONArray) jsonObj.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
mAddress.setLongitude(longitude);
latitude = ((JSONArray) jsonObj.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
mAddress.setLatitude(latitude);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected Address doInBackground(Void... params) {
try {
String SEARCH_TYPE_LAT_LONG = "?latlng=";
String SEARCH_TYPE_ADDRESS = "?address=";
String URL = "https://maps.google.com/maps/api/geocode/json" + (address == null ? SEARCH_TYPE_LAT_LONG : SEARCH_TYPE_ADDRESS) + URLEncoder.encode(latitude + "," + longitude, "utf8") +
"&sensor=false";
Log.d(TAG, "URL: " + URL);
java.net.URL url = new URL(URL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb = sb.append(line).append("\n");
}
jsonObj = new JSONObject(sb.toString());
setAddress();
getGeoPoint();
} catch (Exception e) {
e.printStackTrace();
}
return mAddress;
}
@Override
protected void onPostExecute(Address address) {
super.onPostExecute(address);
onGetAddress(address);
}
abstract void onGetAddress(Address address);
}
class Address {
String Address1 = "";
String Address2 = "";
String City = "";
String State = "";
String Country = "";
String County = "";
String PIN = "";
String Area = "";
private String address;
private double latitude;
private double longitude;
public String getAddress1() {
return Address1;
}
public void setAddress1(String address1) {
Address1 = address1;
}
public String getAddress2() {
return Address2;
}
public void setAddress2(String address2) {
Address2 = address2;
}
public String getCity() {
return City;
}
public void setCity(String city) {
City = city;
}
public String getState() {
return State;
}
public void setState(String state) {
State = state;
}
public String getCountry() {
return Country;
}
public void setCountry(String country) {
Country = country;
}
public String getCounty() {
return County;
}
public void setCounty(String county) {
County = county;
}
public String getPIN() {
return PIN;
}
public void setPIN(String PIN) {
this.PIN = PIN;
}
public String getArea() {
return Area;
}
public void setArea(String area) {
Area = area;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public double getLatitude() {
return latitude;
}
public void setLatitude(double latitude) {
this.latitude = latitude;
}
public double getLongitude() {
return longitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
@Override
public String toString() {
return "Address{" +
"address='" + address + '\'' +
", latitude=" + latitude +
", longitude=" + longitude +
", Address1='" + Address1 + '\'' +
", Address2='" + Address2 + '\'' +
", City='" + City + '\'' +
", State='" + State + '\'' +
", Country='" + Country + '\'' +
", County='" + County + '\'' +
", PIN='" + PIN + '\'' +
", Area='" + Area + '\'' +
'}';
}
}