All files / lib/geocoder virtualearth.js

36.36% Statements 8/22
0% Branches 0/6
0% Functions 0/4
36.36% Lines 8/22
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 661x 1x             1x                 1x     1x             1x                                                 1x                       1x  
var util = require('util');
var AbstractGeocoder = require('./abstractgeocoder');
 
/**
 * Constructor
 * @param <object> httpAdapter Http Adapter
 * @param <object> options     Options (language, clientId, apiKey)
 */
var VirtualEarthGeocoder = function VirtualEarthGeocoder(httpAdapter, options) {
 
  VirtualEarthGeocoder.super_.call(this, httpAdapter, options);
 
  if (!this.options.apiKey || this.options.apiKey == 'undefined') {
    throw new Error('You must specify an apiKey');
  }
};
 
util.inherits(VirtualEarthGeocoder, AbstractGeocoder);
 
// TomTom geocoding API endpoint
VirtualEarthGeocoder.prototype._endpoint = 'http://dev.virtualearth.net/REST/v1/Locations';
 
/**
* Geocode
* @param <string>   value    Value to geocode (Address)
* @param <function> callback Callback method
*/
VirtualEarthGeocoder.prototype._geocode = function(value, callback) {
 
  var _this = this;
 
  var params = {
    q : value,
    key   : this.options.apiKey
  };
 
  this.httpAdapter.get(this._endpoint, params, function(err, result) {
    if (err) {
      return callback(err);
    } else {
      var results = [];
 
      for(var i = 0; i < result.resourceSets[0].resources.length; i++) {
          results.push(_this._formatResult(result.resourceSets[0].resources[i]));
      }
 
      results.raw = result;
      callback(false, results);
    }
  });
};
 
VirtualEarthGeocoder.prototype._formatResult = function(result) {
  return {
    'latitude' : result.point.coordinates[0],
    'longitude' : result.point.coordinates[1],
    'country' : result.address.countryRegion,
    'city' : result.address.locality,
    'state' : result.address.adminDistrict,
    'zipcode' : result.address.postalCode,
    'streetName': result.address.addressLine
  };
};
 
module.exports = VirtualEarthGeocoder;