angular.module('push') .factory('LocationService', function (UtilService, $q, ConfigService) { return { getCurrentLocation: function () { var deferred = $q.defer(); if (device.platform == "Android") { cordova.plugins.baidulocation.getCurrentLocation({}, function (result) { ConfigService.location.lng = result.longitude; ConfigService.location.lat = result.latitude; ConfigService.location.province = result.province; ConfigService.location.city = result.city; deferred.resolve(result); }, function (error) { // console.log(error); deferred.reject(error); }) } else { navigator.geolocation.getCurrentPosition(function (position) { console.log(position); var params = { coordtype: "wgs84ll", location: position.coords.latitude + "," + position.coords.longitude, output: "json", pois: 0, ak: "qqacma5XL7cTDFLD2uBhneeDCOYCQOj1", callback: "renderReverse", mcode: "6E:A5:7B:23:70:FC:7F:4D:70:48:6C:10:DA:C4:26:C0:ED:0E:CD:8F;com.push.pushapp" }; UtilService.post("http://api.map.baidu.com/geocoder/v2/", params).success(function (response) { ConfigService.location.lng = response.result.location.lng; ConfigService.location.lat = response.result.location.lat; ConfigService.location.province = response.result.addressComponent.province; ConfigService.location.city = response.result.addressComponent.city; ConfigService.location.district = response.result.addressComponent.district; console.log(response); deferred.resolve(response); }).error(function (data) { deferred.reject(data); }); }, function (error) { deferred.reject(error); }); } return deferred.promise; } } });