You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.6 KiB

  1. angular.module('app.editTravel', ['pascalprecht.translate', 'ui-leaflet'])
  2. .controller('EditTravelCtrl', function($scope, $stateParams, $translate,
  3. $http, $filter, $ionicLoading) {
  4. $scope.center = {};
  5. $scope.bounds = {};
  6. $scope.markers = [];
  7. $scope.tiles= {
  8. url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
  9. options: {
  10. attribution: '<a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
  11. }
  12. };
  13. $scope.travel={};
  14. // get the travel
  15. $http.get(urlapi + 'travels/id/' + $stateParams.travelid)
  16. .then(function(data) {
  17. console.log('data success travels');
  18. console.log(data); // for browser console
  19. $scope.travel = data.data; // for UI
  20. $scope.travel.date = new Date($scope.travel.date);
  21. $scope.markers = [];
  22. $scope.markers.push({
  23. lat: Number($scope.travel.from.lat),
  24. lng: Number($scope.travel.from.long),
  25. message: $scope.travel.from.name
  26. });
  27. $scope.markers.push({
  28. lat: Number($scope.travel.to.lat),
  29. lng: Number($scope.travel.to.long),
  30. message: $scope.travel.to.name
  31. });
  32. $scope.center = {
  33. lat: (Number($scope.travel.from.lat) + Number($scope.travel.to.lat)) / 2,
  34. lng: (Number($scope.travel.from.long) + Number($scope.travel.to.long)) / 2,
  35. zoom: 4
  36. };
  37. $scope.$broadcast('scroll.refreshComplete'); //refresher stop
  38. }, function(data) {
  39. console.log('data error');
  40. $scope.$broadcast('scroll.refreshComplete'); //refresher stop
  41. $ionicLoading.show({
  42. template: 'Error connecting server',
  43. noBackdrop: true,
  44. duration: 2000
  45. });
  46. });
  47. $scope.updateTravel =function(){
  48. $http({
  49. url: urlapi + 'travels/id/modify/' + $scope.travel._id,
  50. method: "PUT",
  51. data: $scope.travel
  52. })
  53. .then(function(data) {
  54. console.log(data);
  55. window.location="#app/travels/" + $scope.travel._id;
  56. },
  57. function(data) { // optional
  58. // failed
  59. console.log(data);
  60. $ionicLoading.show({ template: 'Error updating travel. ' + data.data, noBackdrop: true, duration: 2000 });
  61. });
  62. };
  63. $scope.getGeo = function(){
  64. $scope.markers=[];
  65. console.log($scope.travel.from.name);
  66. console.log($scope.travel.to.name);
  67. $http.get('https://nominatim.openstreetmap.org/search?q=' + $scope.travel.from.name + '&format=json&limit=1')
  68. .then(function(data) {
  69. console.log(data);
  70. if(data.data[0])
  71. {
  72. $scope.travel.from.lat=data.data[0].lat;
  73. $scope.travel.from.long=data.data[0].lon;
  74. $scope.travel.from.name=data.data[0].display_name;
  75. $scope.markers.push({
  76. lat: Number(data.data[0].lat),
  77. lng: Number(data.data[0].lon),
  78. message: data.data[0].display_name
  79. });
  80. }
  81. $http.get('https://nominatim.openstreetmap.org/search?q=' + $scope.travel.to.name + '&format=json&limit=1')
  82. .then(function(data) {
  83. if(data.data[0])
  84. {
  85. $scope.travel.to.lat=data.data[0].lat;
  86. $scope.travel.to.long=data.data[0].lon;
  87. $scope.travel.to.name=data.data[0].display_name;
  88. $scope.markers.push({
  89. lat: Number(data.data[0].lat),
  90. lng: Number(data.data[0].lon),
  91. message: data.data[0].display_name
  92. });
  93. }
  94. });
  95. });
  96. };
  97. });