# All files / lib/geocoder mapquestgeocoder.js

45.1% Statements 23/51
20.83% Branches 5/24
50% Functions 3/6
45.1% Lines 23/51

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

    2x

    7x

    6x

    1x

    5x

    2x

    2x

    2x
    1x
    1x

    1x

    1x
    1x

    2x

    2x
    1x
    1x

    1x

    1x

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

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

      MapQuestGeocoder.super_.call(this, httpAdapter);

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

        throw new Error('MapQuestGeocoder needs an apiKey');
      }

      this.apiKey = apiKey;
    };

    util.inherits(MapQuestGeocoder, AbstractGeocoder);

    MapQuestGeocoder.prototype._endpoint = 'http://www.mapquestapi.com/geocoding/v1';

    /**
    * Geocode
    * @param <string>   value    Value to geocode (Address)
    * @param <function> callback Callback method
    */
    MapQuestGeocoder.prototype._geocode = function(value, callback) {
      var params = {'key' : querystring.unescape(this.apiKey)};
      Iif (typeof value === 'object') {
        if (value.address) {
          params.street = value.address;
        }
        if (value.country) {
          params.country = value.country;
        }
        if (value.zipcode) {
          params.postalCode = value.zipcode;
        }
        if (value.city) {
          params.city = value.city;
        }
      } else {
        params.location = value;
      }

      var _this = this;
      this.httpAdapter.get(this._endpoint + '/address' , params, 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 = [];
          if (result.results && result.results.length) {
            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) {
      return {
        'latitude' : result.latLng.lat,
        'longitude' : result.latLng.lng,
        'country' : null,
        'city' : result.adminArea5,
        'stateCode' : result.adminArea3,
        'zipcode' : result.postalCode,
        'streetName': result.street,
        'streetNumber' : null,
        'countryCode' : result.adminArea1
      };
    };

    /**
    * 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 = 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;