staticMap.js
3.87 KB
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
/**
* Modules from the community: package.json
*/
var check = require('check-types');
/**
* Internal modules
*/
var _makeRequest = require('./utils/makeRequest');
var _assignParams = require('./utils/assignParams');
var _jsonParser = require('./utils/jsonParser');
var _parseMarkers = require('./utils/parseMarkers');
var _parseStyles = require('./utils/parseStyles');
var _parsePaths = require('./utils/parsePaths');
var _constants = require('./config/constants');
var ACCEPTED_PARAMS = _constants.ACCEPTED_PARAMS;
var GOOGLEMAPS_ENDPOINTS = _constants.GOOGLEMAPS_ENDPOINTS;
var MAX_REQUEST_LENGTHS = _constants.MAX_REQUEST_LENGTHS;
var METHOD_KEY = 'static-map';
function _errorHandler(callback, error) {
if (typeof callback === 'function') {
return callback(error);
}
throw error;
}
module.exports = function(params, callback) {
if (typeof callback !== "undefined" && callback !== null) {
if (typeof callback !== 'function') {
throw new TypeError('callback must be present');
}
}
if (!(this.config.google_client_id && this.config.google_private_key) && this.config.key == null) {
return _errorHandler(callback, new Error('The staticMap API requires a key. You can add it to the config.'));
}
if (!check.object(params)) {
return _errorHandler(callback, new TypeError('params must be an object'));
}
var args = _assignParams({}, params, ACCEPTED_PARAMS[METHOD_KEY]);
// If a marker param or path param is provided then center and zoom param is not required
if (args.markers == null && args.path == null) {
if (args.center == null) {
return _errorHandler(callback, new Error('params.center is required'));
}
if (args.zoom == null) {
return _errorHandler(callback, new Error('params.zoom is required'));
}
}
if (args.markers != null) {
try {
args.markers = _parseMarkers(args.markers);
} catch (ex) {
return _errorHandler(callback, ex);
}
}
if (args.zoom != null) {
if (args.zoom < 0 || args.zoom > 21) {
return _errorHandler(callback, new Error('params.zoom must be between 0 and 21'));
}
}
if (args.size == null || !/^\d+x\d+$/.test(args.size)) {
return _errorHandler(callback, new Error('params.size must be specified in the form {horizontal_value}x{vertical_value}'));
}
if (args.scale != null) {
var scale = args.scale;
if (scale !== 1 && scale !== 2 && scale !== 4) {
return _errorHandler(callback, new Error('params.scale must be 1, 2, 4'));
}
if (scale === 4 && !(this.config.google_client_id && this.config.google_private_key)) {
return _errorHandler(callback, new Error('params.scale can be 4 only for GoogleMaps for work users'));
}
}
if (args.format != null) {
if (args.format != 'png8' && args.format != 'png' && args.format != 'png32' && args.format != 'gif' && args.format != 'jpg' && args.format != 'jpg-baseline') {
return _errorHandler(callback, new Error('Invalid params.format: '+args.format+'. Valid params.format are [png8|png|png32|gif|jpg|jpg-baseline]'));
}
}
if (args.maptype != null) {
args.maptype = args.maptype.toLowerCase();
if (args.maptype !== 'roadmap' && args.maptype !== 'satellite' && args.maptype !== 'terrain' && args.maptype !== 'hybrid') {
return _errorHandler(callback, new Error('Invalid params.maptype: '+args.maptype+'. Valid params.maptype are [roadmap|satellite|terrain|hybrid]'));
}
}
if (args.style != null) {
try {
args.style = _parseStyles(args.style);
} catch (ex) {
return _errorHandler(callback, ex);
}
}
if (args.path != null) {
try {
args.path = _parsePaths(args.path, this.config.encode_polylines);
} catch (ex) {
return _errorHandler(callback, ex);
}
}
return _makeRequest(this.request, this.config, GOOGLEMAPS_ENDPOINTS[METHOD_KEY], args, callback, MAX_REQUEST_LENGTHS[METHOD_KEY], 'binary');
};