unserver-unify
Version:
46 lines • 1.42 kB
JavaScript
angular.module('bamboo').component('simpleMap', {
templateUrl: 'app/directive/others/simplemap.component.html',
bindings: {
postcode: '<',
},
controller: ['$http',function($http) {
var self = this;
var previousInputItem;
self.$onInit = function() {
self.item = {};
console.info(self.postcode);
searchpostcode(self.postcode);
};
//check if the postcode is changed
this.$doCheck = function() {
if (!angular.equals(previousInputItem, self.postcode)) {
previousInputItem = self.postcode;
self.item = {};
searchpostcode(self.postcode);
}
}
//search the location by code
function searchpostcode(postcode) {
var url = "https://maps.google.com/maps/api/geocode/json?address=Sg+" + postcode;
var params = {
url: url,
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
timeout: 1200000,
// async: false,
};
$http.defaults.useXDomain = true;
console.info(params);
$http(params).then(function(result) {
console.log(result);
if (result.status == '200' && result.data.status == 'OK') {
var location = result.data.results[0].geometry.location;
self.item.lat = location.lat;
self.item.lng = location.lng;
}
})
};
}]
});