-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeoclue2demo.py
executable file
·125 lines (95 loc) · 3.98 KB
/
geoclue2demo.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
from gi.repository import GtkClutter, Clutter
GtkClutter.init([]) # Must be initialized before importing those:
from gi.repository import GObject, Gtk, Champlain, GtkChamplain, Pango
import geoclue2client
class Geoclue2Demo:
def __init__(self):
self.window = Gtk.Window()
self.window.set_border_width(10)
self.window.set_title("GeoClue2 + GTK Python Demo")
self.window.connect("destroy", Gtk.main_quit)
vbox = Gtk.VBox(False, 10)
embed = GtkChamplain.Embed()
self.view = embed.get_view()
self.view.set_reactive(True)
self.view.set_property('kinetic-mode', True)
self.view.set_property('zoom-level', 5)
scale = Champlain.Scale()
scale.connect_view(self.view)
self.view.bin_layout_add(scale, Clutter.BinAlignment.START, Clutter.BinAlignment.END)
license = self.view.get_license_actor()
embed.set_size_request(640, 480)
bbox = Gtk.HBox(False, 10)
button = Gtk.Button(stock=Gtk.STOCK_ZOOM_IN)
button.connect("clicked", self.zoom_in)
bbox.add(button)
button = Gtk.Button(stock=Gtk.STOCK_ZOOM_OUT)
button.connect("clicked", self.zoom_out)
bbox.add(button)
button = Gtk.ToggleButton(label="Current Location")
button.set_active(False)
button.connect("toggled", self.get_location)
bbox.add(button)
self.spinbutton = Gtk.SpinButton.new_with_range(0, 20, 1)
self.spinbutton.connect("changed", self.zoom_changed)
self.view.connect("notify::zoom-level", self.map_zoom_changed)
self.spinbutton.set_value(5)
bbox.add(self.spinbutton)
button = Gtk.Image()
self.view.connect("notify::state", self.view_state_changed, button)
bbox.pack_end(button, False, False, 0)
vbox.pack_start(bbox, expand=False, fill=False, padding=0)
vbox.add(embed)
self.window.add(vbox)
self.window.show_all()
def zoom_in(self, widget):
self.view.zoom_in()
def zoom_out(self, widget):
self.view.zoom_out()
def zoom_changed(self, widget):
self.view.set_property("zoom-level", self.spinbutton.get_value_as_int())
def map_zoom_changed(self, widget, value):
self.spinbutton.set_value(self.view.get_property("zoom-level"))
def view_state_changed(self, view, paramspec, image):
state = view.get_state()
if state == Champlain.State.LOADING:
image.set_from_stock(Gtk.STOCK_NETWORK, Gtk.IconSize.BUTTON)
else:
image.clear()
def place_marker(self, latitude, longitude, description):
orange = Clutter.Color.new(0xf3, 0x94, 0x07, 0xbb)
layer = Champlain.MarkerLayer()
markup_string = "";
lines = description.split(',')
if (len(lines) > 1):
markup_string = lines[0] + "\n<span size=\"xx-small\">" + \
", ".join(lines[1:]) + "</span>"
elif (len(lines) == 0):
markup_string = lines[0].strip()
marker = Champlain.Label.new_with_text(
markup_string, "Serif 14", None, orange)
marker.set_use_markup(True)
marker.set_color(orange)
marker.set_location(latitude, longitude)
layer.add_marker(marker)
self.view.center_on(latitude, longitude)
layer.show()
self.view.add_layer(layer)
def get_location(self, widget):
geoclue2client.get_client()
geoclue2client.start_client(self.my_location_handler)
def my_location_handler(self, latitude, longitude, accuracy, description):
print "Latitude = " + str(latitude)
print "Longitude = " + str(longitude)
print "Accuracy = " + str(accuracy)
print "Description = " + str(description)
geoclue2client.stop_client()
self.place_marker(latitude, longitude, description)
if __name__ == "__main__":
GObject.threads_init()
Geoclue2Demo()
Gtk.main()