我正在使用带有phonegap的angular.js。为了获取用户 gps 位置,我开发了一个简单的服务:
app.factory('geolocation', function ($rootScope, cordovaReady) {
return {
getCurrentPosition: cordovaReady(function (onSuccess, onError, options) {
navigator.geolocation.getCurrentPosition(function () {
var that = this,
args = arguments;
if (onSuccess) {
$rootScope.$apply(function () {
onSuccess.apply(that, args);
});
}
}, function () {
var that = this,
args = arguments;
if (onError) {
$rootScope.$apply(function () {
onError.apply(that, args);
});
}
}, options);
});
}
});
我这样称呼这项服务
geolocation.getCurrentPosition(function (position) {
console.log(position);
});
现在我想使用另一种名为getCurrentCity的方法来扩展服务。此方法应使用getCurrentPosition来确定城市。所以服务看起来像这样:
app.factory('geolocation', function ($rootScope, cordovaReady) {
return {
getCurrentPosition: ....
getCurrentCity: ...
}
});
这可能吗?我应该如何使用 getCurrentPosition 来实现 getCurrentCity?