-
Notifications
You must be signed in to change notification settings - Fork 0
/
Geocode.cpp
187 lines (160 loc) · 3.63 KB
/
Geocode.cpp
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
//#include <ESP8266HTTPclient.h>
#include <ArduinoJson.h>
#include "ESP8266WiFi.h"
#include <NtpClientLib.h> // https://github.com/gmag11/NtpClient
#include <JsonStreamingParser.h>
#include "UserConfig.h"
#include "GoogleGeolocation.h"
/**********************************************************
Step 3 - Geocoding
Get Location name from latitude and longitude
**********************************************************/
bool Geocode::acquire(double latitude, double longitude)
{
//Connect to the client and make the api call
if (httpsConnect(FPSTR(maps_Host), FPSTR(maps_fingerprint)))
{
// Serial.println(geocoding_url + String(latitude, 8) + "," + String(longitude, 8) + "&key=" + googleApiKey);
// Concatenate url and key
String url = FPSTR(geocoding_url);
url += String(latitude, 8);
url += F(",");
url += String(longitude, 8);
url += F("&key=" );
url += FPSTR(googleApiKey);
if (httpsGet(FPSTR(maps_Host), url) && skipResponseHeaders())
{
// Invalidate current values
street = FPSTR(empty);
streetNumber = FPSTR(empty);
postCode = FPSTR(empty);
subLocality = FPSTR(empty);
town = FPSTR(empty);
country = FPSTR(empty);
formattedAddress = FPSTR(empty);
while (client.available())
{
JsonStreamingParser parser;
parser.setListener(this);
char c;
int size = 0;
while ((size = client.available()) > 0)
{
c = client.read();
parser.parse(c);
}
}
}
else
// Get failed
return false;
}
else
// Could not connect
return false;
disconnect();
valid = true;
return true;
}
bool Geocode::isValid()
{
return valid;
}
void Geocode::whitespace(char c) {
// Serial.println("whitespace");
}
void Geocode::startDocument() {
// // Serial.println("start document");
}
void Geocode::key(String key)
{
// Serial.println("key: " + key);
currentKey = key;
}
void Geocode::value(String value)
{
// // Serial.println("value: " + value);
if (currentKey == F("long_name"))
savedValue = value;
else if (currentKey == F("types"))
{
if (value == F("route"))
{
if (street == "")
street = savedValue;
}
else if (value == F("street_number"))
{
if (streetNumber == "")
streetNumber = savedValue;
}
else if (value == F("postal_code"))
{
if (postCode == "")
postCode = savedValue;
}
else if (value == F("sublocality"))
{
if (country == "")
subLocality = savedValue;
}
else if (value == F("locality"))
{
if (town == "")
town = savedValue;
}
else if (value == F("country"))
{
if (country == "")
country = savedValue;
}
else if (value == F("formatted_address"))
{
if (formattedAddress == "")
formattedAddress = savedValue;
}
}
}
void Geocode::endArray() {
// Serial.println("end array. ");
}
void Geocode::endObject() {
// // Serial.println("end object. ");
}
void Geocode::endDocument() {
// // Serial.println("end document. ");
}
void Geocode::startArray() {
// // Serial.println("start array. ");
}
void Geocode::startObject() {
// // Serial.println("start object. ");
}
String Geocode::getStreet()
{
return street;
}
String Geocode::getStreetNumber()
{
return streetNumber;
}
String Geocode::getPostCode()
{
return postCode;
}
String Geocode::getSubLocality()
{
return subLocality;
}
String Geocode::getTown()
{
return town;
}
String Geocode::getCountry()
{
return country;
}
String Geocode::getFormattedAddress()
{
return formattedAddress;
}