-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathangular-geocomplete.js
More file actions
136 lines (127 loc) · 4.39 KB
/
angular-geocomplete.js
File metadata and controls
136 lines (127 loc) · 4.39 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// geocomplete
// ===================
//
(function () {
'use strict';
angular.module('geocomplete', [])
.factory('geoComplete', [ '$http', function ($http) {
var apiUrl = 'https://maps.googleapis.com/maps/api/geocode/json';
/* Public API */
return {
//
// <a name='cities'></a>
// Public: cities
// --------------------------
//
// #### Arguments
//
// + `cityName`: `String`
// + `options`: (Optional )`Object` of options, see more: https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
// + `callback`: (Optional) `Callback` function
//
// #### Returns
//
// An `Array` with a `String` with the name of the City followed by the
// State and the Country name, separated by commas.
//
// #### Example on a REPL:
//
// ```
// $ utils.autocompleteCities("San ");
// => ["San Diego, CA, United States", "San Andreas, CA, United States", ...]
// ```
//
cities: function (cityName, options, callback) {
return $http.get(apiUrl, {
params: angular.extend({}, options, {
address: cityName,
sensor: false
})
}).then(function (res) {
var addresses = [];
angular.forEach(res.data.results, function (item) {
addresses.push(item.formatted_address)
});
if (callback) {
return callback.call(this, addresses);
}
return addresses;
});
},
//
// <a name='citiesJSON'></a>
// Public: geocomplete Cities returning a JSON
// --------------------------------------------
//
// Just like autocompleteCities but instead of returning an Array with
// Strings it returns an Array with a JSON with more details per city.
//
// #### Arguments
//
// + `cityName`: A `String` with the city/place name
// + `options`: (Optional )`Object` of options, see more: https://developers.google.com/maps/documentation/geocoding/intro#ComponentFiltering
// + `callback`: (Optional) `Callback` function.
//
// #### Returns
//
// An `Array` with an `Object` with the full response from Google's API:
//
// #### Example on REPL:
//
// ```
// $ utils.autocompleteCitiesJSON("San Cristobal");
// => [{
// "address_components":[
// {
// "long_name":"San Cristobal",
// "short_name":"San Cristobal",
// "types":["locality","political"]
// }, {
// "long_name":"San Cristobal",
// "short_name":"San Cristobal",
// "types":["administrative_area_level_2","political"]
// }, {
// "long_name":"Táchira",
// "short_name":"Táchira",
// "types":["administrative_area_level_1","political"]
// }, {
// "long_name":"Venezuela",
// "short_name":"VE",
// "types":["country","political"]
// }
// ],
// "formatted_address":"San Cristobal, Venezuela",
// "geometry":{
// "bounds":{
// "northeast":{"lat":7.816616199999999,"lng":-72.19365119999999},
// "southwest":{"lat":7.718986699999999,"lng":-72.2554064}
// },
// "location":{"lat":7.764951000000001,"lng":-72.226061},
// "location_type":"APPROXIMATE",
// "viewport":{"northeast":{"lat":7.816616199999999,"lng":-72.19365119999999},"southwest":{"lat":7.718986699999999,"lng":-72.2554064}}
// },
// "types":["locality","political"]
// }, ...]
// ```
//
citiesJSON: function (cityName, options, callback) {
return $http.get(apiUrl, {
params: angular.extend({}, options, {
address: cityName,
sensor: false
})
}).then(function (res) {
var addresses = [];
angular.forEach(res.data.results, function (item) {
addresses.push(item);
});
if (callback) {
return callback.call(this, addresses);
}
return addresses;
});
}
};
}]);
})();