zettapi_client
Version:
Client side CRUD operations in angular to use with zettapi_server rest api to get started quickly in any CMS project
66 lines (56 loc) • 1.43 kB
JavaScript
app.service('$address', function($http, blockUI) {
this.getAddressPT = function(zipcode, callback) {
if (this.validateZipcode(zipcode)) {
return callback("Código postal inválido");
}
blockUI.start('A procurar arruamento...');
$http.get("./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;
}
if (cps.length > 1) {
if (isNaN(cps[1])) {
return true;
}
}
return false;
};
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) {
callback(status);
}
callback(null, {
lat: results[0].geometry.location.lat(),
lng: results[0].geometry.location.lng()
});
});
}
catch (error) {
callback(error);
}
};
});