~ubuntu-branches/ubuntu/vivid/gnome-maps/vivid-proposed

« back to all changes in this revision

Viewing changes to src/mapLocation.js

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson
  • Date: 2013-10-15 22:08:33 UTC
  • Revision ID: package-import@ubuntu.com-20131015220833-rqoyqqu1wp0qrblw
Tags: upstream-3.10.0
Import upstream version 3.10.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
 
2
/* vim: set et ts=4 sw=4: */
 
3
/*
 
4
 * Copyright (c) 2011, 2012, 2013 Red Hat, Inc.
 
5
 *
 
6
 * GNOME Maps is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by the
 
8
 * Free Software Foundation; either version 2 of the License, or (at your
 
9
 * option) any later version.
 
10
 *
 
11
 * GNOME Maps is distributed in the hope that it will be useful, but
 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
13
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
14
 * for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License along
 
17
 * with GNOME Maps; if not, write to the Free Software Foundation,
 
18
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 *
 
20
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 
21
 */
 
22
 
 
23
const Clutter = imports.gi.Clutter;
 
24
const Champlain = imports.gi.Champlain;
 
25
const Geocode = imports.gi.GeocodeGlib;
 
26
 
 
27
const Lang = imports.lang;
 
28
const Mainloop = imports.mainloop;
 
29
const Signals = imports.signals;
 
30
 
 
31
const Utils = imports.utils;
 
32
const Path = imports.path;
 
33
const _ = imports.gettext.gettext;
 
34
 
 
35
// A map location object with an added accuracy.
 
36
const MapLocation = new Lang.Class({
 
37
    Name: 'MapLocation',
 
38
 
 
39
    _init: function(geocodeLocation, mapView) {
 
40
        this._mapView = mapView;
 
41
        this._view = mapView.view;
 
42
        this.latitude = geocodeLocation.latitude;
 
43
        this.longitude = geocodeLocation.longitude;
 
44
        this.description = geocodeLocation.description;
 
45
        this.accuracy = geocodeLocation.accuracy;
 
46
    },
 
47
 
 
48
    // Go to this location from the current location on the map, optionally
 
49
    // with an animation
 
50
    // TODO: break this out somewhere, this is useful in other cases as well.
 
51
    goTo: function(animate) {
 
52
        Utils.debug("Going to " + this.description);
 
53
 
 
54
        if (!animate) {
 
55
            this._view.center_on(this.latitude, this.longitude);
 
56
            this.zoomToFit();
 
57
            this.emit('gone-to');
 
58
 
 
59
            return;
 
60
        }
 
61
 
 
62
        /* Lets first ensure that both current and destination location are visible
 
63
         * before we start the animated journey towards destination itself. We do this
 
64
         * to create the zoom-out-then-zoom-in effect that many map implementations
 
65
         * do. This not only makes the go-to animation look a lot better visually but
 
66
         * also give user a good idea of where the destination is compared to current
 
67
         * location.
 
68
         */
 
69
 
 
70
        Utils.once(this._view, "animation-completed", (function() {
 
71
            Utils.once(this._view, "animation-completed::go-to", (function() {
 
72
                this.zoomToFit();
 
73
                this.emit('gone-to');
 
74
            }).bind(this));
 
75
 
 
76
            this._view.go_to(this.latitude, this.longitude);
 
77
        }).bind(this));
 
78
 
 
79
        this._mapView.ensureVisible([this._getCurrentLocation(), this]);
 
80
    },
 
81
 
 
82
    show: function(layer) {
 
83
        let marker = new Champlain.Label({ text: this.description });
 
84
        marker.set_location(this.latitude, this.longitude);
 
85
        layer.add_marker(marker);
 
86
        Utils.debug("Added marker at " + this.latitude + ", " + this.longitude);
 
87
    },
 
88
 
 
89
    showNGoTo: function(animate, layer) {
 
90
        this.show(layer);
 
91
        this.goTo(animate);
 
92
    },
 
93
 
 
94
    // Zoom to the maximal zoom-level that fits the accuracy circle
 
95
    zoomToFit: function() {
 
96
        let zoom;
 
97
        if (this.accuracy === Geocode.LOCATION_ACCURACY_UNKNOWN)
 
98
            zoom = 11; // Accuracy is usually city-level when unknown
 
99
        else if (this.accuracy <= Geocode.LOCATION_ACCURACY_STREET)
 
100
            zoom = 16;
 
101
        else if (this.accuracy <= Geocode.LOCATION_ACCURACY_CITY)
 
102
            zoom = 11;
 
103
        else if (this.accuracy <= Geocode.LOCATION_ACCURACY_REGION)
 
104
            zoom = 10;
 
105
        else if (this.accuracy <= Geocode.LOCATION_ACCURACY_COUNTRY)
 
106
            zoom = 6;
 
107
        else
 
108
            zoom = 3;
 
109
        this._view.set_zoom_level(zoom);
 
110
    },
 
111
 
 
112
    getAccuracyDescription: function() {
 
113
        switch(this.accuracy) {
 
114
        case Geocode.LOCATION_ACCURACY_UNKNOWN:
 
115
            /* Translators: Accuracy of user location information */
 
116
            return _("Unknown");
 
117
        case 0:
 
118
            /* Translators: Accuracy of user location information */
 
119
            return _("Exact");
 
120
        default:
 
121
            let area =  Math.PI * Math.pow(this.accuracy / 1000, 2);
 
122
            area = Math.floor(area);
 
123
            return area.toString() + _(" km²");
 
124
        }
 
125
    },
 
126
 
 
127
    _getCurrentLocation: function() {
 
128
        return new Geocode.Location({
 
129
            latitude: this._view.get_center_latitude(),
 
130
            longitude: this._view.get_center_longitude()
 
131
        });
 
132
    }
 
133
});
 
134
Utils.addSignalMethods(MapLocation.prototype);