Skip to content

Instantly share code, notes, and snippets.

@avantassel
Created October 20, 2015 03:22
Show Gist options
  • Save avantassel/d303c9c0ec2ce69c9923 to your computer and use it in GitHub Desktop.
Save avantassel/d303c9c0ec2ce69c9923 to your computer and use it in GitHub Desktop.
Google Geolocation Strongloop Datasource
"geo": {
"name": "geo",
"connector": "rest",
"operations": [
{
"template": {
"method": "GET",
"url": "http://maps.googleapis.com/maps/api/geocode/{format=json}",
"headers": {
"accepts": "application/json",
"content-type": "application/json"
},
"query": {
"address": "{street},{city},{zipcode}",
"sensor": "{sensor=false}"
},
"responsePath": "$.results[0].geometry.location"
},
"functions": {
"geocode": [
"street",
"city",
"zipcode"
]
}
}
]
}
@avantassel
Copy link
Author

Here's how to call it from a Strongloop model with a vow promise

// Google Maps API has a rate limit of 10 requests per second
  // Seems we need to enforce a lower rate to prevent errors
  var lookupGeo = require('function-rate-limit')(5, 1000, function() {
    var geoService = Model.app.dataSources.geo;
    geoService.geocode.apply(geoService, arguments);
  });

  function getGeo(address){
    var deferred = vow.defer();
     lookupGeo(address.street, address.city, address.zip, function(err, result) {
         if (result && result.length) {
           deferred.resolve( [result[0].lng,result[0].lat] );
         } else {
           deferred.reject( 'No Geo Found' );
         }
       });
    return deferred.promise();
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment