The GoogleMaps class is an Electric Imp agent side library aimed at accessing the Google Maps API.
To add this library to your project, add #require "GoogleMaps.agent.nut:1.0.0"
to the top of your agent code and #require "GoogleMaps.device.nut:1.0.0"
to the top of your device code.
You can view the library's source code on GitHub.
See Google's API documentation for further information.
Apply for an API key on the Google Developer Console
This will request a wifi scan from the device by issuing a device.send()
command. The device will respond with the wifi networks it can see. It will retry as many times as it needs until it gets a response from the device.
This will take the wifi data from scanWifi()
and send it to the Google Maps geolocation API. This API will try to return a geolocation based on the wifi signals it can see. The result will be returned as a table in the callback with fields:
Field | Meaning |
---|---|
lat | Latitude |
lng | Longitude |
accuracy | The accuracy radius of the estimated location, in meters |
This will take the location data from geolocate()
and send it to the Google Maps timezone API. This API will try to return a timezone data based on the geolocation provided. The result will be returned as a table in the callback with the following fields:
Field | Meaning |
---|---|
status | Status of the API query. Either OK or FAIL. |
timeZoneId | The name of the time zone. Refer to time zone list. |
timeZoneName | The long description of the time zone |
gmtOffsetStr | GMT Offset String such as GMT-7 |
rawOffset | The time zone's offset without DST changes |
dstOffset | The DST offset to be added to the rawOffset to get the current gmtOffset |
gmtOffset | The time offset in seconds based on UTC time. |
time | Current local time in Unix timestamp. |
date | Squirrel date() object |
dateStr | Date string formatted as YYYY-MM-DD HH-MM-SS |
#require "GoogleMaps.agent.nut:1.0.0"
gm <- GoogleMaps("apikey");
device.onconnect(function() {
imp.wakeup(1, function() {
gm.scanWifi(function(err, wifis) {
if (err) return server.log(err);
gm.geolocate(wifis, function(err, location) {
if (err) return server.log(err);
gm.timezone(location, function(err, tzdata) {
if (err) return server.log(err);
server.log(format("[%f,%f] +/- %dm ==> %s %s", location.lat, location.lng, location.accuracy, tzdata.dateStr, tzdata.timeZoneId));
});
});
});
});
});
#require "GoogleMaps.device.nut:1.0.0"
The GoogleMaps class is licensed under the MIT License.
The example code errors out if device is connected when build and run is hit (agent/device race condition is started). Might be better to replace device.onconnect with imp.wakeup for the example code.