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.0
52import QtQuick.Window 2.0
53import QtLocation 5.12
54import QtPositioning 5.12
55import QtQuick.Shapes 1.1
56
57Window {
58    width: 512
59    height: 512
60    visible: true
61
62    Plugin {
63        id: mapPlugin
64        name: "osm"
65    }
66
67    Map {
68        id: map
69        visible: true
70        anchors.fill: parent
71        plugin: mapPlugin
72        center: QtPositioning.coordinate(59.91, 10.75) // Oslo
73        zoomLevel: 9.5
74
75        onMapReadyChanged: miv.model = osloListModel
76
77        Text {
78            anchors.top: parent.top
79            anchors.topMargin: 20
80            anchors.horizontalCenter: parent.horizontalCenter
81            text: "Long press to drop a marker"
82            color: "grey"
83            font.pixelSize: 24
84            opacity: 0.8
85            z: parent.z + 10
86        }
87
88        MouseArea {
89            anchors.fill: parent
90            onPressAndHold: {
91                var crd = map.toCoordinate(Qt.point(mouseX, mouseY))
92                console.log(crd)
93                markerModel.clear()
94                markerModel.append({ "latitude": crd.latitude, "longitude": crd.longitude})
95            }
96        }
97
98        //! [MarkerView]
99        MapItemView {
100            id: mivMarker
101
102            add: Transition {
103                NumberAnimation {
104                    property: "slideIn"
105                    from: 50
106                    to: 0
107                    duration: 500
108                    easing.type: Easing.OutBounce
109                    easing.amplitude: 3.0
110                }
111            }
112
113            remove: Transition {
114                NumberAnimation {
115                    property: "opacity"
116                    to: 0.1
117                    duration: 50
118                }
119            }
120
121            model: ListModel {
122                id: markerModel
123            }
124            delegate: Component {
125                MapQuickItem {
126                    coordinate: QtPositioning.coordinate(latitude, longitude)
127                    anchorPoint: Qt.point(e1.width * 0.5, e1.height + slideIn)
128                    property real slideIn : 0
129                    sourceItem: Shape {
130                        id: e1
131                        vendorExtensionsEnabled: false
132                        width: 32
133                        height: 32
134                        visible: true
135
136                        transform: Scale {
137                            origin.y: e1.height * 0.5
138                            yScale: -1
139                        }
140
141                        ShapePath {
142                            id: c_sp1
143                            strokeWidth: -1
144                            fillColor: Qt.rgba(1,0,1,1.0)
145
146                            property real half: e1.width * 0.5
147                            property real quarter: e1.width * 0.25
148                            property point center: Qt.point(e1.x + e1.width * 0.5 , e1.y + e1.height * 0.5)
149
150
151                            property point top: Qt.point(center.x, center.y - half )
152                            property point bottomLeft: Qt.point(center.x - half, center.y + half )
153                            property point bottomRight: Qt.point(center.x + half, center.y + half )
154
155                            startX: center.x;
156                            startY: center.y + half
157
158                            PathLine { x: c_sp1.bottomLeft.x; y: c_sp1.bottomLeft.y }
159                            PathLine { x: c_sp1.top.x; y: c_sp1.top.y }
160                            PathLine { x: c_sp1.bottomRight.x; y: c_sp1.bottomRight.y }
161                            PathLine { x: c_sp1.center.x; y: c_sp1.center.y + c_sp1.half }
162                        }
163                    }
164                }
165            }
166        }
167        //! [MarkerView]
168
169        //! [OsloAdministrativeRegions]
170        MapItemView {
171            id: miv
172            model: OsloListModel {
173                id: osloListModel
174            }
175            add: Transition {
176                NumberAnimation {
177                    property: "animationScale"
178                    from: 0.2
179                    to: 1
180                    duration: 800
181                    easing.type: Easing.OutCubic
182                }
183            }
184            delegate: Component {
185                MapPolygon {
186                    function fromMercator(l, centroid) {
187                        var res = []
188                        for (var i  = 0; i < l.length; i++) {
189                            var vtx = l[i]
190                            var offset = Qt.point((vtx.x - centroid.x) * animationScale,
191                                                  (vtx.y - centroid.y) * animationScale)
192                            var pt = Qt.point(centroid.x + offset.x, centroid.y + offset.y)
193                            res.push( QtPositioning.mercatorToCoord(pt) )
194                        }
195                        return res;
196                    }
197
198                    path: fromMercator(osloListModel.geometries[name+"_"+adminLevel]
199                                       , osloListModel.centroids[name+"_"+adminLevel] )
200                    color: ((adminLevel > 4) ? "lightsteelblue" : 'firebrick')
201                    property real animationScale : 1
202                    opacity: ((adminLevel < 9) ? 0.1 : 0.8)
203                    visible: true
204                }
205            }
206        }
207        //! [OsloAdministrativeRegions]
208    }
209}
210