All files / lib/geocoder heregeocoder.js

94.38% Statements 84/89
70.59% Branches 48/68
100% Functions 7/7
94.38% Lines 84/89
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 1972x 2x             2x 29x   29x   28x 3x       2x     2x     2x             2x   10x 10x   10x 2x     2x     2x 2x 2x     2x     2x 1x   2x   8x     10x 3x 3x   3x 1x   2x 2x         2x 2x   2x         2x 14x         14x 14x   14x 14x   14x 1x   14x 1x   14x 2x   14x 2x     14x     2x 2x 2x     2x                                         2x 9x 9x     2x 2x     2x 2x     2x 2x 2x     2x 2x       2x               2x 4x 4x   4x 4x 4x 4x   4x 3x 3x   3x 1x   2x 2x         2x 2x   2x         2x  
var util = require('util'),
  AbstractGeocoder = require('./abstractgeocoder');
 
/**
 * Constructor
 * @param <object> httpAdapter Http Adapter
 * @param <object> options     Options (appId, appCode, language, politicalView, country, state)
 */
var HereGeocoder = function HereGeocoder(httpAdapter, options) {
  this.options = ['appId', 'appCode', 'language', 'politicalView', 'country', 'state'];
 
  HereGeocoder.super_.call(this, httpAdapter, options);
 
  if (!this.options.appId || !this.options.appCode) {
    throw new Error('You must specify appId and appCode to use Here Geocoder');
  }
};
 
util.inherits(HereGeocoder, AbstractGeocoder);
 
// Here geocoding API endpoint
HereGeocoder.prototype._geocodeEndpoint = 'https://geocoder.cit.api.here.com/6.2/geocode.json';
 
// Here reverse geocoding API endpoint
HereGeocoder.prototype._reverseEndpoint = 'https://reverse.geocoder.api.here.com/6.2/reversegeocode.json';
 
/**
 * Geocode
 * @param <string>   value    Value ton geocode (Address)
 * @param <function> callback Callback method
 */
HereGeocoder.prototype._geocode = function (value, callback) {
 
  var _this = this;
  var params = this._prepareQueryString();
 
  if (value.address) {
    Iif (value.language) {
        params.language = value.language;
    }
    Iif (value.politicalView) {
        params.politicalview = value.politicalView;
    }
    Eif (value.country) {
        params.country = value.country;
        Iif (value.state) {
            params.state = value.state;
        } else {
            delete params.state;
        }
    }
    if (value.zipcode) {
        params.postalcode = value.zipcode;
    }
    params.searchtext = value.address;
  } else {
    params.searchtext = value;
  }
 
  this.httpAdapter.get(this._geocodeEndpoint, params, function (err, result) {
    var results = [];
    results.raw = result;
 
    if (err) {
      return callback(err, results);
    } else {
      var view = result.Response.View[0];
      Iif (!view) {
        return callback(false, results);
      }
 
      // Format each geocoding result
      results = view.Result.map(_this._formatResult);
      results.raw = result;
 
      callback(false, results);
    }
  });
};
 
HereGeocoder.prototype._prepareQueryString = function () {
  var params = {
    'additionaldata': 'Country2,true',
    'gen': 8
  };
 
  Eif (this.options.appId) {
    params.app_id = this.options.appId;
  }
  Eif (this.options.appCode) {
    params.app_code = this.options.appCode;
  }
  if (this.options.language) {
    params.language = this.options.language;
  }
  if (this.options.politicalView) {
    params.politicalview = this.options.politicalView;
  }
  if (this.options.country) {
    params.country = this.options.country;
  }
  if (this.options.state) {
    params.state = this.options.state;
  }
 
  return params;
};
 
HereGeocoder.prototype._formatResult = function (result) {
  var location = result.Location || {};
  var address = location.Address || {};
  var i;
 
  var extractedObj = {
    formattedAddress: address.Label || null,
    latitude: location.DisplayPosition.Latitude,
    longitude: location.DisplayPosition.Longitude,
    country: null,
    countryCode: address.Country || null,
    state: address.State || null,
    county: address.County || null,
    city: address.City || null,
    zipcode: address.PostalCode || null,
    district: address.District || null,
    streetName: address.Street || null,
    streetNumber: address.HouseNumber || null,
    building: address.Building || null,
    extra: {
      herePlaceId: location.LocationId || null,
      confidence: result.Relevance || 0
    },
    administrativeLevels: {}
  };
 
  for (i = 0; i < address.AdditionalData.length; i++) {
    var additionalData = address.AdditionalData[i];
    switch (additionalData.key) {
      //Country 2-digit code
      case 'Country2':
        extractedObj.countryCode = additionalData.value;
        break;
      //Country name
      case 'CountryName':
        extractedObj.country = additionalData.value;
        break;
      //State name
      case 'StateName':
        extractedObj.administrativeLevels.level1long = additionalData.value;
        extractedObj.state = additionalData.value;
        break;
      //County name
      case 'CountyName':
        extractedObj.administrativeLevels.level2long = additionalData.value;
        extractedObj.county = additionalData.value;
    }
  }
 
  return extractedObj;
};
 
/**
 * Reverse geocoding
 * @param {lat:<number>,lon:<number>}  lat: Latitude, lon: Longitude
 * @param <function> callback Callback method
 */
HereGeocoder.prototype._reverse = function (query, callback) {
  var lat = query.lat;
  var lng = query.lon;
 
  var _this = this;
  var params = this._prepareQueryString();
  params.pos = lat + ',' + lng;
  params.mode = 'trackPosition';
 
  this.httpAdapter.get(this._reverseEndpoint, params, function (err, result) {
    var results = [];
    results.raw = result;
 
    if (err) {
      return callback(err, results);
    } else {
      var view = result.Response.View[0];
      Iif (!view) {
        return callback(false, results);
      }
 
      // Format each geocoding result
      results = view.Result.map(_this._formatResult);
      results.raw = result;
 
      callback(false, results);
    }
  });
};
 
module.exports = HereGeocoder;