summaryrefslogtreecommitdiff
path: root/src/mapWalker.js
blob: 09195f8ecaca17c17febf94d6e65afa65a3c2601 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
/* vim: set et ts=4 sw=4: */
/*
 * Copyright (c) 2011, 2012, 2013 Red Hat, Inc.
 * 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: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *         Damián Nohales <damiannohales@gmail.com>
 */

import GObject from 'gi://GObject';

import {BoundingBox} from './boundingBox.js';
import {Location} from './location.js';
import * as PlaceZoom from './placeZoom.js';
import * as Utils from './utils.js';

const _MAX_DISTANCE = 19850; // half of Earth's circumference (km)
const _MIN_ANIMATION_DURATION = 2000; // msec
const _MAX_ANIMATION_DURATION = 5000; // msec

export class MapWalker extends GObject.Object {

    constructor(place, mapView) {
        super();
        this.place = place;
        this._mapView = mapView;
        this._viewport = mapView.map.viewport;
        this._boundingBox = this._createBoundingBox(this.place);
    }

    _createBoundingBox(place) {
        if (place.bounding_box !== null) {
            return new BoundingBox({ top: place.bounding_box.top,
                                     bottom: place.bounding_box.bottom,
                                     left: place.bounding_box.left,
                                     right: place.bounding_box.right });
        } else {
            return null;
        }
    }

    _getZoomLevel() {
        let zoom;

        if (this.place.initialZoom) {
            zoom = this.place.initialZoom;
        } else {
            zoom = PlaceZoom.getZoomLevelForPlace(this.place) ??
                   this._viewport.max_zoom_level;

            /* If the place has a bounding box, use the lower of the default
             * zoom level based on the place's type and the zoom level needed
             * fit the bounding box. This way we ensure the bounding box will
             * be all visible and we also have an appropriate amount
             * of context for the place
             */
            if (this._boundingBox !== null && this._boundingBox.isValid()) {
                let bboxZoom =
                    this._mapView.getZoomLevelFittingBBox(this._boundingBox);

                zoom = Math.min(zoom, bboxZoom);
            }
        }

        return zoom;
    }

    // Zoom to the maximal zoom-level that fits the place type
    zoomToFit() {
        let zoom = this._getZoomLevel();

         /*
         * WORKAROUND!!!!
         *
         * For now disable the animation to prevent getting
         * throttled by the tile server:
         * https://gitlab.gnome.org/GNOME/gnome-maps/-/issues/546
         */
        this._mapView.map.center_on(this.place.location.latitude,
                                    this.place.location.longitude);
        this._mapView.map.viewport.zoom_level = zoom;
    }

    goTo(animate, linear) {
        let zoom = this._getZoomLevel();
        Utils.debug('Going to ' + [this.place.name,
                    this.place.location.latitude,
                    this.place.location.longitude].join(' '));
        this._mapView.emit('going-to');

        /*
         * WORKAROUND!!!!
         *
         * For now disable the go-to animation to prevent getting
         * throttled by the tile server:
         * https://gitlab.gnome.org/GNOME/gnome-maps/-/issues/546
         */
        animate = false;

        if (!animate) {
            this._mapView.map.center_on(this.place.location.latitude,
                                        this.place.location.longitude);
            this._mapView.map.viewport.zoom_level = zoom;
            this.emit('gone-to');
            return;
        } else {
            this._mapView.map.go_to_full(this.place.location.latitude,
                                         this.place.location.longitude,
                                         zoom);
            Utils.once(this._mapView.map, 'animation-completed::go-to',
                       () => this.emit('gone-to'));
        }
    }

    _ensureVisible(fromLocation) {
        let visibleBox = null;

        if (this._boundingBox !== null && this._boundingBox.isValid()) {
            visibleBox = this._boundingBox.copy();

            visibleBox.extend(fromLocation.latitude, fromLocation.longitude);
        } else {
            visibleBox = new BoundingBox({ left:   180,
                                           right: -180,
                                           bottom:  90,
                                           top:    -90 });

            [fromLocation, this.place.location].forEach((location) => {
                visibleBox.left   = Math.min(visibleBox.left,   location.longitude);
                visibleBox.right  = Math.max(visibleBox.right,  location.longitude);
                visibleBox.bottom = Math.min(visibleBox.bottom, location.latitude);
                visibleBox.top    = Math.max(visibleBox.top,    location.latitude);
            });
        }

        let [lon, lat] = visibleBox.getCenter();

        this._view.zoom_level = this._mapView.getZoomLevelFittingBBox(visibleBox);
        this._view.go_to(lat, lon);
    }

    _boxCovers(coverBox) {
        if (this._boundingBox === null)
            return false;

        if (coverBox.left > this._boundingBox.left)
            return false;

        if (coverBox.right < this._boundingBox.right)
            return false;

        if (coverBox.top < this._boundingBox.top)
            return false;

        if (coverBox.bottom > this._boundingBox.bottom)
            return false;

        return true;
    }
}

GObject.registerClass({
    Signals: {
        'gone-to': { }
    }
}, MapWalker);