File: gps.py

package info (click to toggle)
python-plyer 2.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,808 kB
  • sloc: python: 13,395; sh: 217; makefile: 177
file content (96 lines) | stat: -rw-r--r-- 2,635 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
'''
GPS
====

.. versionadded:: 1.1

.. note::
    On Android `INTERNET`, `ACCESS_FINE_LOCATION`, `ACCESS_COARSE_LOCATION`
    permissions are needed.


.. note::
    On iOS `NSLocationWhenInUseUsageDescription` key is required for app to
    display geolocation usage permission prompt. Key can be added in Xcode
    target `info` section or in ``Resources/<YourApp>-info.plist``.
    App background mode (`on_pause`) also must be supported.

You need to set a `on_location` callback with the :meth:`configure` method.
This callback will receive a couple of keywords / values, that might be
different depending of their availability on the targeted platform.
Lat and lon are always available.

- lat: latitude of the last location, in degrees
- lon: longitude of the last location, in degrees
- speed: speed of the user, in meters/second over ground
- bearing: bearing in degrees
- altitude: altitude in meters above the sea level

Here is an example of the usage of gps::

    from plyer import gps

    def print_locations(**kwargs):
        print 'lat: {lat}, lon: {lon}'.format(**kwargs)

    gps.configure(on_location=print_locations)
    gps.start()
    # later
    gps.stop()

Supported Platforms
-------------------
Android, iOS

'''


class GPS:
    '''
    GPS facade.
    '''

    def configure(self, on_location, on_status=None):
        '''
        Configure the GPS object. This method should be called before
        :meth:`start`.

        :param on_location: Function to call when receiving a new location
        :param on_status: Function to call when a status message is received
        :type on_location: callable, multiples keys/value will be passed.
        :type on_status: callable, args are "message-type", "status"

        .. warning::

            The `on_location` and `on_status` callables might be called from
            another thread than the thread used for creating the GPS object.
        '''
        self.on_location = on_location
        self.on_status = on_status
        self._configure()

    def start(self, minTime=1000, minDistance=1):
        '''
        Start the GPS location updates.
        Expects 2 parameters:
            minTime: milliseconds.  (float)
            minDistance: meters. (float)
        '''
        self._start(minTime=minTime, minDistance=minDistance)

    def stop(self):
        '''
        Stop the GPS location updates.
        '''
        self._stop()

    # private

    def _configure(self):
        raise NotImplementedError()

    def _start(self, **kwargs):
        raise NotImplementedError()

    def _stop(self):
        raise NotImplementedError()