# All files / lib/geocoder openmapquestgeocoder.js

42.86% Statements 18/42
25% Branches 4/16
33.33% Functions 2/6
42.86% Lines 18/42

    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
    1242x
    2x
    2x

    2x

    6x

    5x

    1x

    4x
    4x

    2x

    2x

    2x

    2x
    1x
    1x

    1x

    1x

    2x
     var querystring = require('querystring'),
      util = require('util'),
      AbstractGeocoder = require('./abstractgeocoder');

    /**
     * Constructor
     */
    var MapQuestGeocoder = function OpenMapQuestGeocoder(httpAdapter, apiKey) {

      MapQuestGeocoder.super_.call(this, httpAdapter);

      if (!apiKey || apiKey == 'undefined') {

        throw new Error(this.constructor.name + ' needs an apiKey');
      }

      this.apiKey = apiKey;
      this._endpoint = 'http://open.mapquestapi.com/geocoding/v1';
    };

    util.inherits(MapQuestGeocoder, AbstractGeocoder);

    /**
     * Geocode
     * @param <string>   value    Value to geocode (Address)
     * @param <function> callback Callback method
     */
    MapQuestGeocoder.prototype._geocode = function (value, callback) {
      var _this = this;
      this.httpAdapter.get(this._endpoint + '/address', {
        'location': value,
        'key': querystring.unescape(this.apiKey)
      }, function (err, result) {
        if (err) {
          return callback(err);
        } else {
          if (result.info.statuscode !== 0) {
            return callback(new Error('Status is ' + result.info.statuscode + ' ' + result.info.messages[0]), {raw: result});
          }

          var results = [];

          var locations = result.results[0].locations;

          for (var i = 0; i < locations.length; i++) {
            results.push(_this._formatResult(locations[i]));
          }

          results.raw = result;
          callback(false, results);
        }
      });
    };

    MapQuestGeocoder.prototype._formatResult = function (result) {
      var MQConfidenceLookup = {
        POINT: 1,
        ADDRESS: 0.9,
        INTERSECTION: 0.8, //less accurate than the MQ description
        STREET: 0.7,
        ZIP: 0.5,
        ZIP_EXTENDED: 0.5,
        NEIGHBORHOOD: 0.5,
        CITY: 0.4,
        COUNTY: 0.3,
        STATE: 0.2,
        COUNTRY: 0.1
      };
      return {
        'latitude': result.latLng.lat,
        'longitude': result.latLng.lng,
        'country': null,
        'countryCode': result.adminArea1,
        'city': result.adminArea5,
        'state': result.adminArea3,
        'zipcode': result.postalCode,
        'streetName': result.street,
        'streetNumber': null,
        'extra': {
          confidence: MQConfidenceLookup[result.geocodeQuality] || 0
        }

      };
    };

    /**
     * Reverse geocoding
     * @param {lat:<number>,lon:<number>}  lat: Latitude, lon: Longitude
     * @param <function> callback Callback method
     */
    MapQuestGeocoder.prototype._reverse = function (query, callback) {
      var lat = query.lat;
      var lng = query.lon;

      var _this = this;

      this.httpAdapter.get(this._endpoint + '/reverse', {
        'location': lat + ',' + lng,
        'key': querystring.unescape(this.apiKey)
      }, function (err, result) {
        if (err) {
          return callback(err);
        } else {
          var results = [];
          var locations;

          if (result.results === undefined || !result.results.length) {
              return callback(new Error('Incorrect response'));
          }

          locations = result.results[0].locations;

          for (var i = 0; i < locations.length; i++) {
            results.push(_this._formatResult(locations[i]));
          }

          results.raw = result;
          callback(false, results);
        }
      });
    };

    module.exports = MapQuestGeocoder;