Overview

This solutions document will go over how to install and use Foundry's Geocoding Service. This service uses Google's Geocoding API to convert addresses to Longitude and Latitude and vice versa. It can also be used in called from either another Lambda function or with a Global Action.


Requirments

Google Developer API Key

Foundry's Geocoding service rely's on Google's Geolocation API to convert the addresses or longitude and latitude. To use Google's Geolocation API you must first sign-up for a Google Developer account and select the API's you want to have access to which can be done by using this link.


When selecting the products you want to sign-up for check the Places product.


Installing the Lambda Function

Once you have generated your API key it is no time to install your Lambda function.

  1. Log In to the AWS account that you have your Foundry instance installed in and navigate to the Cloud Formation service.
  2. In the upper left select "Create Stack".
  3. Select "Upload a template to Amazon S3" and Upload the JSON file "geocoding_cf_template.json" which is attached in this solutions. Once uploaded navigate to the next page.
  4. In the next page fill out the Stack Name and parameters. 
    • Environment: dev, stage, or prod
    • GoogeAPIKey: API Key you generated in the previous steps.                                                                                            
  5. Continue and navigate to the Review page, check the box at the bottom, and then hit Create.
  6. Once you Template has reached the stage "CREATE_COMPLETE" the Lambda function is now ready to use.


Calling the Lambda Function

You can use the newly created Lambda function in two ways; by either creating a global action to call it or calling it directly from another Lambda function.

Invoking from Another Lambda function

By invoking your newly created Lambda function from another Lambda function you have two options with what to do with the returned data. You can either pass a "deviceId" of the Object you want the attributes to be populated created in the payload or use the returned data in your own Lambda function.


Example Converting Address to Longitude and Latitude:

CLIENT_LAMBDA = boto3.client('lambda')

payload {
  'deviceId': '12345-678-91011',
  'address': '1600 Amphitheatre Parkway, Mountain View, CA 94043, USA'
}


CLIENT_LAMBDA.invoke(
    FunctionName='fndy-lat-long-address-dev',
    InvocationType='Event',
    Payload=json.dumps(payload)
)


Expected Payload Returned to Lambda:

{
   "lat" : 37.4224764,
   "lng" : -122.0842499
}



Example Converting Longitude and Latitude to an Address (Without deviceId):

CLIENT_LAMBDA = boto3.client('lambda')

payload {
  'lat': '37.4238253802915',
  'lon': '-122.0829009197085'
}


CLIENT_LAMBDA.invoke(
    FunctionName='fndy-lat-long-address-dev',
    InvocationType='Event',
    Payload=json.dumps(payload)
)




Expected Payload Returned to Lambda:

"1600 Amphitheatre Parkway, Mountain View, CA 94043, USA"



Invoking from a Global Action

To invoke the Lambda function from a Global Action you must pass the "deviceId" in the payload. For more on how to create Global actions use this link. The payloads are exactly the same as invoking from a Lambda function.


Example of Global Action: