|
| 1 | +#! /usr/bin/env -S vala workbench.vala --pkg libadwaita-1 --pkg shumate-1.0 |
| 2 | + |
| 3 | +private Shumate.SimpleMap map_widget; |
| 4 | +private Shumate.Viewport viewport; |
| 5 | +private Gtk.Entry entry_latitude; |
| 6 | +private Gtk.Entry entry_longitude; |
| 7 | + |
| 8 | + |
| 9 | +void go_to_location () { |
| 10 | + float latitude = 0, longitude = 0; |
| 11 | + |
| 12 | + if (!float.try_parse (entry_latitude.text, out latitude) || !float.try_parse |
| 13 | + (entry_longitude.text, out longitude)) { |
| 14 | + message (@"Please enter valid coordinates"); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + if (latitude > Shumate.MAX_LATITUDE || latitude < Shumate.MIN_LATITUDE) { |
| 19 | + message ( |
| 20 | + @"Latitudes must be between $(Shumate.MIN_LATITUDE) and $(Shumate.MAX_LATITUDE)"); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if (longitude > Shumate.MAX_LONGITUDE || longitude < Shumate.MIN_LONGITUDE) { |
| 25 | + message ( |
| 26 | + @"Longitudes must be between $(Shumate.MIN_LONGITUDE) and $(Shumate.MAX_LONGITUDE)" |
| 27 | + ); |
| 28 | + return; |
| 29 | + } |
| 30 | + viewport.zoom_level = 5; |
| 31 | + map_widget.map.go_to (latitude, longitude); |
| 32 | +} |
| 33 | + |
| 34 | +public void main () { |
| 35 | + map_widget = (Shumate.SimpleMap) workbench.builder.get_object ("map_widget"); |
| 36 | + var registry = new Shumate.MapSourceRegistry.with_defaults (); |
| 37 | + |
| 38 | + // Use OpenStreetMap as the source |
| 39 | + var map_source = registry.get_by_id (Shumate.MAP_SOURCE_OSM_MAPNIK); |
| 40 | + viewport = map_widget.viewport; |
| 41 | + |
| 42 | + map_widget.map_source = map_source; |
| 43 | + map_widget.map.center_on (0, 0); |
| 44 | + |
| 45 | + // Reference map source used by MarkerLayer |
| 46 | + viewport.reference_map_source = map_source; |
| 47 | + viewport.zoom_level = 5; |
| 48 | + |
| 49 | + var marker_layer = new Shumate.MarkerLayer (viewport) { |
| 50 | + selection_mode = SINGLE |
| 51 | + }; |
| 52 | + |
| 53 | + var marker = (Shumate.Marker) workbench.builder.get_object ("marker"); |
| 54 | + marker.set_location (0, 0); |
| 55 | + marker_layer.add_marker (marker); |
| 56 | + map_widget.map.add_layer (marker_layer); |
| 57 | + |
| 58 | + var gesture = new Gtk.GestureClick (); |
| 59 | + map_widget.add_controller (gesture); |
| 60 | + |
| 61 | + var button_marker = (Gtk.ToggleButton) workbench.builder.get_object ("button_marker"); |
| 62 | + |
| 63 | + gesture.pressed.connect ((self, n_press, x, y) => { |
| 64 | + if (button_marker.active) { |
| 65 | + double latitude, longitude; |
| 66 | + viewport.widget_coords_to_location (map_widget, x, y, out latitude, out longitude); |
| 67 | + marker.set_location (latitude, longitude); |
| 68 | + message (@"Marker placed at $(latitude), $(longitude)"); |
| 69 | + } |
| 70 | + }); |
| 71 | + |
| 72 | + entry_latitude = (Gtk.Entry) workbench.builder.get_object ("entry_latitude"); |
| 73 | + entry_longitude = (Gtk.Entry) workbench.builder.get_object ("entry_longitude"); |
| 74 | + var button_go = (Gtk.Button) workbench.builder.get_object ("button_go"); |
| 75 | + |
| 76 | + button_go.clicked.connect (() => { |
| 77 | + go_to_location (); |
| 78 | + }); |
| 79 | + |
| 80 | + entry_latitude.activate.connect (() => { |
| 81 | + go_to_location (); |
| 82 | + }); |
| 83 | + |
| 84 | + entry_longitude.activate.connect (() => { |
| 85 | + go_to_location (); |
| 86 | + }); |
| 87 | +} |
0 commit comments