summaryrefslogtreecommitdiff
path: root/src/userLocationMarker.js
blob: fbc4a85194b49964d74a4e0c8c76c46fd54f01e6 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2014 Damián Nohales
 *
 * GNOME Maps is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * GNOME Maps is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with GNOME Maps; if not, see <http://www.gnu.org/licenses/>.
 *
 * Author: Damián Nohales <damiannohales@gmail.com>
 */

import Cairo from 'cairo';
import GObject from 'gi://GObject';
import Graphene from 'gi://Graphene';
import Shumate from 'gi://Shumate';

import {MapMarker} from './mapMarker.js';

export class AccuracyCircleMarker extends Shumate.Marker {

    constructor({place, ...params}) {
        super({
            ...params,
            latitude: place.location.latitude,
            longitude: place.location.longitude,
        });

        this._place = place;
    }

    refreshGeometry(mapView) {
        this.latitude = this._place.location.latitude;
        this.longitude = this._place.location.longitude;

        let zoom = mapView.map.viewport.zoom_level;
        let source = mapView.mapSource;
        let metersPerPixel = source.get_meters_per_pixel(zoom,
                                                         this.latitude,
                                                         this.longitude);
        let size = this._place.location.accuracy * 2 / metersPerPixel;
        let {x, y, width, height} = mapView.get_allocation();

        if (size > width || size > height)
            this.visible = false;
        else {
            this.set_size_request(size, size);
            this.visible = true;
            this.queue_draw();
        }
    }

    vfunc_snapshot(snapshot) {
        let {x, y, width, height} = this.get_allocation();
        let rect = new Graphene.Rect();

        rect.init(0, 0, width, height);

        let cr = snapshot.append_cairo(rect);

        cr.setOperator(Cairo.Operator.OVER);

        cr.setSourceRGBA(0, 0, 255, 0.1);
        cr.arc(width / 2, width / 2, width / 2, 0, Math.PI * 2);
        cr.fillPreserve();

        super.vfunc_snapshot(snapshot);
    }
}

GObject.registerClass(AccuracyCircleMarker);

export class UserLocationMarker extends MapMarker {

    constructor(params) {
        super(params);

        this._accuracyMarker = new AccuracyCircleMarker({ place: this.place });
        this.connect('notify::view-zoom-level',
                     () => this._accuracyMarker.refreshGeometry(this._mapView));
        this._mapView.connect('notify::default-width',
                     () => this._accuracyMarker.refreshGeometry(this._mapView));
        this._mapView.connect('notify::default-height',
                     () => this._accuracyMarker.refreshGeometry(this._mapView));
        this._accuracyMarker.refreshGeometry(this._mapView);

        this.place.connect('notify::location', () => this._updateLocation());
        this._image.pixel_size = 24;
        this._updateLocation();

        this.connect('notify::visible', this._updateAccuracyCircle.bind(this));
        this._mapView.map.viewport.connect('notify::rotation',
                                           () => this._updateLocation());
    }

    _hasBubble() {
        return true;
    }

    addToLayer(layer) {
        layer.add_marker(this._accuracyMarker);
        layer.add_marker(this);
    }

    _updateLocation() {
        if (this.place.location.heading > -1) {
            this._image.icon_name = 'user-location-compass'
            this.queue_draw();
        } else {
            this._image.icon_name = 'user-location';
        }

        this._updateAccuracyCircle();
    }

    _updateAccuracyCircle() {
        if (this.visible && this.place.location.accuracy > 0) {
            this._accuracyMarker.refreshGeometry(this._mapView);
        } else {
            this._accuracyMarker.visible = false;
        }
    }

    vfunc_snapshot(snapshot) {
        snapshot.save();

        if (this.place.location.heading > -1) {
            // rotate around the center of the icon
            let width = this.get_width();
            let height = this.get_height();
            let point = new Graphene.Point();
            let rotation = this.place.location.heading +
                           this._mapView.map.viewport.rotation * 180 / Math.PI;

            point.init(width / 2, height / 2);
            snapshot.translate(point);
            snapshot.rotate(rotation);
            point.init(-width / 2, -height / 2);
            snapshot.translate(point);
        }

        this.snapshot_child(this._image, snapshot);
        super.vfunc_snapshot(snapshot);
        snapshot.restore();
    }
}

GObject.registerClass(UserLocationMarker);