LocationService.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. angular.module('push')
  2. .factory('LocationService', function (UtilService, $q, ConfigService) {
  3. return {
  4. getCurrentLocation: function () {
  5. var deferred = $q.defer();
  6. if (device.platform == "Android") {
  7. cordova.plugins.baidulocation.getCurrentLocation({}, function (result) {
  8. ConfigService.location.lng = result.longitude;
  9. ConfigService.location.lat = result.latitude;
  10. ConfigService.location.province = result.province;
  11. ConfigService.location.city = result.city;
  12. deferred.resolve(result);
  13. }, function (error) {
  14. // console.log(error);
  15. deferred.reject(error);
  16. })
  17. } else {
  18. navigator.geolocation.getCurrentPosition(function (position) {
  19. console.log(position);
  20. var params = {
  21. coordtype: "wgs84ll",
  22. location: position.coords.latitude + "," + position.coords.longitude,
  23. output: "json",
  24. pois: 0,
  25. ak: "qqacma5XL7cTDFLD2uBhneeDCOYCQOj1",
  26. callback: "renderReverse",
  27. mcode: "6E:A5:7B:23:70:FC:7F:4D:70:48:6C:10:DA:C4:26:C0:ED:0E:CD:8F;com.push.pushapp"
  28. };
  29. UtilService.post("http://api.map.baidu.com/geocoder/v2/", params).success(function (response) {
  30. ConfigService.location.lng = response.result.location.lng;
  31. ConfigService.location.lat = response.result.location.lat;
  32. ConfigService.location.province = response.result.addressComponent.province;
  33. ConfigService.location.city = response.result.addressComponent.city;
  34. ConfigService.location.district = response.result.addressComponent.district;
  35. console.log(response);
  36. deferred.resolve(response);
  37. }).error(function (data) {
  38. deferred.reject(data);
  39. });
  40. }, function (error) {
  41. deferred.reject(error);
  42. });
  43. }
  44. return deferred.promise;
  45. }
  46. }
  47. });