openEventsPlatformApp/www/js/newEvent.js
www/js/newEvent.jsRaw
1angular.module('app.newEvent', ['pascalprecht.translate', 'ui-leaflet'])
2
3.controller('NewEventCtrl', function($scope, $http, $ionicModal, $timeout,
4 $ionicLoading, $filter, leafletData, leafletBoundsHelpers,
5 $cordovaCamera) {
6
7
8 //initialization
9 $scope.event={};
10 $scope.event.categories=[];
11 $scope.event.location={
12 direction: "",
13 geo: {
14 lat: "",
15 long: "",
16 name: ""
17 }
18 };
19
20 //imgfile
21 $scope.selectImgFile = function() {
22 console.log("img");
23 var options = {
24 quality: 100,
25 destinationType: Camera.DestinationType.DATA_URL,
26 sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
27 allowEdit: true,
28 encodingType: Camera.EncodingType.PNG,
29 targetWidth: 500,
30 targetHeight: 500,
31 popoverOptions: CameraPopoverOptions,
32 saveToPhotoAlbum: false,
33 correctOrientation: true
34 };
35
36 $cordovaCamera.getPicture(options).then(function(imageData) {
37 $scope.event.imgpreview = "data:image/jpeg;base64," + imageData;
38 $scope.event.img = imageData;
39 }, function(err) {
40 console.log(err);
41 });
42 };
43
44 //get list of categories
45 $scope.categories;
46 $http.get(urlapi + 'categoriesList')
47 .then(function(data){
48 $scope.categories=data.data;
49 console.log($scope.categories);
50 }, function(data){
51 console.log('data error');
52 console.log(data);
53 $ionicLoading.show({ template: 'Error connecting server', noBackdrop: true, duration: 2000 });
54
55 });
56 $scope.categorySelected = function(){
57 $scope.event.categories=[];
58 for(var i=0; i<$scope.categories.length; i++)
59 {
60 if($scope.categories[i].selected)
61 {
62 $scope.event.categories.push($scope.categories[i]);
63 }
64 }
65 console.log($scope.event.categories);
66 };
67
68
69 $scope.postEvent = function(){
70 $http({
71 url: urlapi + 'events',
72 method: "POST",
73 data: $scope.event
74 })
75 .then(function(data) {
76 $scope.event={};
77 window.location.href="#/app/events";
78 },
79 function(response) { // optional
80 // failed
81 console.log(response);
82 });
83 };
84
85 /* map */
86
87 $scope.center= {
88 lat: 0,
89 lng: 0,
90 zoom: 1
91 };
92 $scope.markers=new Array();
93 $scope.tiles= {
94 url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
95 options: {
96 attribution: '<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
97 }
98 };
99 $scope.getGeo = function(){
100
101 $scope.markers=[];
102 console.log($scope.event.location.direction);
103 $http.get('http://nominatim.openstreetmap.org/search?q=' + $scope.event.location.direction + '&format=json&limit=1')
104 .then(function(data) {
105 console.log(data);
106 if(data.data[0])
107 {
108 $scope.event.location.geo.lat=data.data[0].lat;
109 $scope.event.location.geo.long=data.data[0].lon;
110 //$scope.newtravel.from.name=data.data[0].display_name;
111 $scope.markers.push({
112 lat: Number(data.data[0].lat),
113 lng: Number(data.data[0].lon),
114 message: $scope.event.location.direction
115 });
116 $scope.center= {
117 lat: Number(data.data[0].lat),
118 lng: Number(data.data[0].lon),
119 zoom: 16
120 };
121 }
122 });
123
124 };
125});