Skip to main content

Google Maps

Latest Version: 1.0.1

This library uses the Google Maps API to obtain geolocation and time zone information based on a scan of WiFi networks around the device. Please see Google’s API documentation for further information.

To use the library, you will need a Google API key. You can apply for an API key on the Google Developer Console.

You can view the library’s source code on GitHub. Click here to see information on the available versions of this library.

To include this library in your project, add

#require "GoogleMaps.agent.lib.nut:1.0.1"

at the top of your agent code.

Class Usage

Constructor(apiKey)

The library takes one parameter: your Google API key.

#require "GoogleMaps.agent.lib.nut:1.0.1"

const API_KEY = "<YOUR_API_KEY_HERE>";
gmaps <- GoogleMaps(API_KEY);

Class Methods

getGeolocation(networks, callback)

The getGeolocation() method will try to determine the location of your device based on a device-side scan of nearby WiFi networks. The scan is made by an imp API imp.scanwifinetworks() call, the results of which should be sent to the agent and passed into getGeolocation()’s networks parameter.

The callback parameter is a function that will be called when Google returns location data or an error has occurred. The function has two parameters: error and results. If an error occurred while processing the request, error will contain a description of the error, otherwise it will be null and a table containing the results from Google will be passed into results. This table will contain the following keys:

Key Description
location A table with keys lat and lng
accuracy The accuracy radius of the estimated location, in meters

Example

// Device-side code
agent.send("wifi.networks", imp.scanwifinetworks());
// Agent-side code
const API_KEY = "<YOUR_API_KEY_HERE>";
gmaps <- GoogleMaps(API_KEY);

device.on("wifi.networks", function(networks) {
    gmaps.getGeolocation(networks, function(error, resp) {
        if (error != null) {
            server.error(error);
        } else {
            server.log(format("Location latitude: %f, longitude: %f with accuracy: %f", resp.location.lat, resp.location.lng, resp.accuracy));
        }
    });
});

getTimezone(location, callback)

This method takes two required parameters: a location table with keys lat and lng, and a callback function. It sends the location data to the Google Maps timezone API. The result is passed to the callback function, which takes two parameters: error and results. If an error occurred while processing the request, error will contain a description of the error, otherwise it will be null and a table containing the results from Google will be passed into results. This table will contain the following keys:

Key Description
status Status of the API query. Either "OK" or "FAIL"
timeZoneId The name of the timezone. Refer to Google’s timezone list
timeZoneName A long description of the timezone
gmtOffsetStr GMT offset as a string, eg. "GMT-7"
rawOffset The timezone’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 The request date as a Squirrel date() object
dateStr The request date as a string in YYYY-MM-DD HH-MM-SS format

Example

gmaps.getGeolocation(networks, function(error, resp) {
   if (error != null) {
        server.error(error);
   } else {
        gmaps.getTimezone(resp.location, function(err, res) {
            if (err != null) {
                server.error(err);
            } else {
                server.log(res.timeZoneName);
                server.log(res.dateStr);
            }
        });
   }
});

Release History

The Electric Imp Dev Center documents the latest version of the library. For past versions, please see the Electric Imp public GitHub repos listed below.

Version Source Code Notes
1.0.0 GitHub Initial release
1.0.1 GitHub Fixed a typo in a constant name (runtime issue)

License

This library is licensed under the MIT License.