1/****************************************************************************
2**
3** Copyright (C) 2017 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the examples of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24**   * Redistributions of source code must retain the above copyright
25**     notice, this list of conditions and the following disclaimer.
26**   * Redistributions in binary form must reproduce the above copyright
27**     notice, this list of conditions and the following disclaimer in
28**     the documentation and/or other materials provided with the
29**     distribution.
30**   * Neither the name of The Qt Company Ltd nor the names of its
31**     contributors may be used to endorse or promote products derived
32**     from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51import QtQuick 2.5
52import QtQuick.Controls 1.4
53import QtPositioning 5.5
54import QtLocation 5.6
55import "../helper.js" as Helper
56
57Map {
58    id: map
59    property bool followme: false
60    property variant scaleLengths: [5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000]
61
62    function calculateScale()
63    {
64        var coord1, coord2, dist, text, f
65        f = 0
66        coord1 = map.toCoordinate(Qt.point(0,scale.y))
67        coord2 = map.toCoordinate(Qt.point(0+scaleImage.sourceSize.width,scale.y))
68        dist = Math.round(coord1.distanceTo(coord2))
69
70        if (dist === 0) {
71            // not visible
72        } else {
73            for (var i = 0; i < scaleLengths.length-1; i++) {
74                if (dist < (scaleLengths[i] + scaleLengths[i+1]) / 2 ) {
75                    f = scaleLengths[i] / dist
76                    dist = scaleLengths[i]
77                    break;
78                }
79            }
80            if (f === 0) {
81                f = dist / scaleLengths[i]
82                dist = scaleLengths[i]
83            }
84        }
85
86        text = Helper.formatDistance(dist)
87        scaleImage.width = (scaleImage.sourceSize.width * f) - 2 * scaleImageLeft.sourceSize.width
88        scaleText.text = text
89    }
90
91    center {
92        // The Qt Company in Oslo
93        latitude: 59.9485
94        longitude: 10.7686
95    }
96
97    gesture.flickDeceleration: 3000
98    gesture.enabled: true
99    onCopyrightLinkActivated: Qt.openUrlExternally(link)
100
101    onCenterChanged:{
102        scaleTimer.restart()
103        if (map.followme)
104            if (map.center != positionSource.position.coordinate) map.followme = false
105    }
106
107    onZoomLevelChanged:{
108        scaleTimer.restart()
109        if (map.followme) map.center = positionSource.position.coordinate
110    }
111
112    onWidthChanged:{
113        scaleTimer.restart()
114    }
115
116    onHeightChanged:{
117        scaleTimer.restart()
118    }
119
120    Keys.onPressed: {
121        if (event.key === Qt.Key_Plus) {
122            map.zoomLevel++
123        } else if (event.key === Qt.Key_Minus) {
124            map.zoomLevel--
125        }
126    }
127
128    Timer {
129        id: scaleTimer
130        interval: 100
131        running: false
132        repeat: false
133        onTriggered: {
134            map.calculateScale()
135        }
136    }
137
138    Item {
139        id: scale
140        visible: scaleText.text != "0 m"
141        z: map.z + 3
142        anchors.bottom: parent.bottom
143        anchors.right: parent.right
144        anchors.margins: 20
145        height: scaleText.height * 2
146        width: scaleImage.width
147
148        Image {
149            id: scaleImageLeft
150            source: "../../resources/scale_end.png"
151            anchors.bottom: parent.bottom
152            anchors.right: scaleImage.left
153        }
154        Image {
155            id: scaleImage
156            source: "../../resources/scale.png"
157            anchors.bottom: parent.bottom
158            anchors.right: scaleImageRight.left
159        }
160        Image {
161            id: scaleImageRight
162            source: "../../resources/scale_end.png"
163            anchors.bottom: parent.bottom
164            anchors.right: parent.right
165        }
166        Label {
167            id: scaleText
168            color: "#004EAE"
169            anchors.centerIn: parent
170            text: "0 m"
171        }
172        Component.onCompleted: {
173            map.calculateScale();
174        }
175    }
176
177    MapQuickItem {
178        id: poiTheQtComapny
179        sourceItem: Rectangle { width: 14; height: 14; color: "#e41e25"; border.width: 2; border.color: "white"; smooth: true; radius: 7 }
180        coordinate {
181            latitude: 59.9485
182            longitude: 10.7686
183        }
184        opacity:1.0
185        anchorPoint: Qt.point(sourceItem.width/2, sourceItem.height/2)
186    }
187
188    MapQuickItem {
189        sourceItem: Text{
190            text: "The Qt Company"
191            color:"#242424"
192            font.bold: true
193            styleColor: "#ECECEC"
194            style: Text.Outline
195        }
196        coordinate: poiTheQtComapny.coordinate
197        anchorPoint: Qt.point(-poiTheQtComapny.sourceItem.width * 0.5,poiTheQtComapny.sourceItem.height * 1.5)
198    }
199
200    PositionSource{
201        id: positionSource
202        active: followme
203
204        onPositionChanged: {
205            map.center = positionSource.position.coordinate
206        }
207    }
208
209    Slider {
210        id: zoomSlider;
211        z: map.z + 3
212        minimumValue: map.minimumZoomLevel;
213        maximumValue: map.maximumZoomLevel;
214        anchors.margins: 10
215        anchors.bottom: scale.top
216        anchors.top: parent.top
217        anchors.right: parent.right
218        orientation : Qt.Vertical
219        value: map.zoomLevel
220        onValueChanged: {
221            map.zoomLevel = value
222        }
223    }
224}
225