UNPKG

unserver-unify

Version:

142 lines (138 loc) 4.71 kB
angular.module('bamboo.company').controller('CompanyMapCtrl', function( $http, ApiService, $state, CommonService) { var self = this; console.log("---- CompanyMapCtrl -----"); this.showMap = "all"; this.postcode; console.log($state.current.name); this.items = []; this.companyInfo = {}; this.markerlat; this.markerlng; this.companys = []; this.mysrclat = 1.318938; this.mysrclong = 103.8926112; var resultReady = false; var listReady = false; function calculateDistance() { if (!resultReady || !listReady) { return; } angular.forEach(self.items, function(item, index) { if (item.lat) { item.dis = calDistance(item.lat, item.lng, self.mysrclat, self.mysrclong); } }) } this.searchostcode = function() { if (!self.postcode || self.postcode.length < 6) { CommonService.showError('Invalid postcode'); } var url = "https://maps.google.com/maps/api/geocode/json?address=Sg+" + self.postcode; var params = { url: url, method: 'GET', headers: { 'Content-Type': 'application/json' }, timeout: 1200000, // async: false, }; $http.defaults.useXDomain = true; $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.mysrclat = location.lat; self.mysrclong = location.lng; resultReady = true; calculateDistance(); } }) } this.hightLight = function(item,index) { self.mysrclat = self.markerlat = item.lat; self.mysrclong = self.markerlng = item.lng; self.current=index; } function calDistance(lat1, lon1, lat2, lon2) { var R = 6371; // Radius of the earth in km var dLat = deg2rad(lat2 - lat1); // deg2rad below var dLon = deg2rad(lon2 - lon1); var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); var d = R * c; // Distance in km return d; } function deg2rad(deg) { return deg * (Math.PI / 180) } this.googleMapsUrl = "https://maps.googleapis.com/maps/api/js?key=AIzaSyCDpjAui7Bp-lfs3akUG4zGId3RAyhGA1M"; if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(function(position) { console.log(position); self.mysrclat = position.coords.latitude; self.mysrclong = position.coords.longitude; resultReady = true; calculateDistance(); }, function(err) { console.log(err); // CommonService.showError('Can not locate the user position, use default position'); calculateDistance(); }); } function getitems() { var info = { start: 0, limit: 50, action: "getmapcompanys" }; ApiService.post("/business", info).then(function(result) { // console.log(result); if (result.data.success) { self.items = result.data.data.items; listReady = true; calculateDistance(); console.log(self.items); angular.forEach(result.data.data.items, function(item, index) { // console.log(item); if (item.lat) { var size = 20; /* switch (item.size) { case '21 - 99': size = 99; break; case '100 - 499': size = 499; break; case '500 - 1999': size = 1000; break; case '2000 - 9999': size = 2000; break; case '> 100000': size = 10000; break; default: size=20; }*/ self.companyInfo[item.name] = { /* size:Math.sqrt(Math.sqrt(size||50))*200, user:Math.sqrt(item.users||2)*300, jobs:Math.sqrt(item.jobs||2)*300, apps:Math.sqrt(item.applications||2)*200, counter:Math.sqrt(item.counter)*100, product:Math.sqrt(item.products||2)*300, project:Math.sqrt(item.projects||2)*300, course:Math.sqrt(item.courses||2)*300,*/ pos: [item.lat, item.lng], name: item.name, } } }); console.log(self.companyInfo); } }); }; getitems(); });