geolocation.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. var exec = cordova.require('cordova/exec'); // eslint-disable-line no-undef
  22. var utils = require('cordova/utils');
  23. var PositionError = require('./PositionError');
  24. // Native watchPosition method is called async after permissions prompt.
  25. // So we use additional map and own ids to return watch id synchronously.
  26. var pluginToNativeWatchMap = {};
  27. module.exports = {
  28. getCurrentPosition: function (success, error, args) {
  29. var win = function () {
  30. var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation'); // eslint-disable-line no-undef
  31. geo.getCurrentPosition(success, error, args);
  32. };
  33. var fail = function () {
  34. if (error) {
  35. error(new PositionError(PositionError.PERMISSION_DENIED, 'Illegal Access'));
  36. }
  37. };
  38. exec(win, fail, 'Geolocation', 'getPermission', []);
  39. },
  40. watchPosition: function (success, error, args) {
  41. var pluginWatchId = utils.createUUID();
  42. var win = function () {
  43. var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation'); // eslint-disable-line no-undef
  44. pluginToNativeWatchMap[pluginWatchId] = geo.watchPosition(success, error, args);
  45. };
  46. var fail = function () {
  47. if (error) {
  48. error(new PositionError(PositionError.PERMISSION_DENIED, 'Illegal Access'));
  49. }
  50. };
  51. exec(win, fail, 'Geolocation', 'getPermission', []);
  52. return pluginWatchId;
  53. },
  54. clearWatch: function (pluginWatchId) {
  55. var win = function () {
  56. var nativeWatchId = pluginToNativeWatchMap[pluginWatchId];
  57. var geo = cordova.require('cordova/modulemapper').getOriginalSymbol(window, 'navigator.geolocation'); // eslint-disable-line no-undef
  58. geo.clearWatch(nativeWatchId);
  59. };
  60. exec(win, null, 'Geolocation', 'getPermission', []);
  61. }
  62. };