zettapi_client
Version:
Admin panel and client-side CRUD operations in angular to use with zettapi_server rest api to get started quickly in any CMS project.
40 lines (35 loc) • 1.44 kB
JavaScript
app.service('$address', function($http, blockUI, zapi) {
this.getAddressPT = function(zipcode, callback) {
if (this.validateZipcode(zipcode)) return callback("Código postal inválido");
blockUI.start('A procurar arruamento...');
$http.get(zapi.serverUrl + "/api/address/pt/" + zipcode).then(function(response) {
callback(null, response.data);
}).catch(function(response) {
callback(response);
}).finally(function() {
blockUI.stop();
});
};
this.validateZipcode = function(zipcode) {
if (!zipcode) return true;
if (typeof zipcode !== 'string') zipcode = zipcode + '';
var cps = zipcode.split('-');
if (cps.length < 1 || cps.length > 2) return true;
if (isNaN(cps[0])) return true;
return cps.length > 1 && isNaN(cps[1]);
};
this.getCoordinates = function(zipcode, callback) {
try {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': zipcode }, function(results, status) {
if (status !== google.maps.GeocoderStatus.OK) return callback(status);
callback(null, {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng()
});
});
} catch (error) {
callback(error);
}
};
});