aboutsummaryrefslogtreecommitdiffstats
path: root/app/qml/MapWindow.qml
blob: 8a41390938955caabf1fb11822484cefbcfde89d (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import QtLocation 5.9
import QtPositioning 5.0
import QtQuick 2.0

import com.mapbox.cheap_ruler 1.0

Item {
    id: mapWindow

    property int disOffset: 70
    property real rotateAngle: 0
    property var startPoint
    property var endPoint

    //turn by turn board view
    TbtBoard {
        id: tbt_board
        z: 1
        visible: false
        anchors.fill: parent
    }

    //mapview and route views
    Map {
        id: map
        anchors.fill: parent

        plugin: Plugin {
            name: "mapboxgl"

            PluginParameter {
                name: "mapboxgl.mapping.items.insert_before"
                value: "road-label-small"
            }

            PluginParameter {
                name: "mapboxgl.mapping.additional_style_urls"
                value: "mapbox://styles/mapbox/streets-v9"
            }

            PluginParameter {
                name: "mapboxgl.access_token"
                value: fileOperation.getMapAccessToken()
            }

            PluginParameter {
                name: "mapboxgl.mapping.cache.directory"
                value: "/home/0/app-data/navigation/"
            }
        }

        center: ruler.currentPosition
        zoomLevel: 20
        tilt: 60
        gesture.acceptedGestures:MapGestureArea.NoGesture
        copyrightsVisible: false

        RotationAnimation on bearing {
            id: bearingAnimation

            duration: 250
            alwaysRunToEnd: false
            direction: RotationAnimation.Shortest
            running: true
        }

        Location {
            id: previousLocation
            coordinate: QtPositioning.coordinate(0, 0);
        }

        onCenterChanged: {
            if (previousLocation.coordinate === center)
                return;

            bearingAnimation.to = previousLocation.coordinate.azimuthTo(center);
            bearingAnimation.start();

            previousLocation.coordinate = center;
        }

        MapQuickItem {
            id: startMarker

            sourceItem: Image {
                id: greenMarker
                source: "qrc:///marker-green.png"
            }
            anchorPoint.x: greenMarker.width / 2
            anchorPoint.y: greenMarker.height / 2
        }

        MapQuickItem {
            id: endMarker

            sourceItem: Image {
                id: redMarker
                source: "qrc:///marker-end.png"
            }
            anchorPoint.x: redMarker.width / 2
            anchorPoint.y: redMarker.height / 2
        }

        MapItemView {
            model: routeModel

            delegate: MapRoute {
                route: routeData
                line.color: "#6b43a1"
                line.width: map.zoomLevel - 5
                opacity: (index == 0) ? 1.0 : 0.3

                onRouteChanged: {
                    ruler.path = routeData.path;
                }
            }
        }

        MapQuickItem {
            zoomLevel: map.zoomLevel

            sourceItem: Image {
                id: carMarker
                source: "qrc:///car-marker.png"
                transform: Rotation {
                                origin.x: carMarker.width / 2;
                                origin.y: carMarker.height / 2;
                                angle: rotateAngle
                            }
            }

            coordinate: ruler.currentPosition
            anchorPoint.x: carMarker.width / 2
            anchorPoint.y: carMarker.height / 2

            Location {
                id: previousCarLocation
                coordinate: QtPositioning.coordinate(0, 0);
            }

            onCoordinateChanged: {
                if(coordinate === mapWindow.startPoint)
                    return;
                rotateAngle = previousCarLocation.coordinate.azimuthTo(coordinate);
                previousCarLocation.coordinate = coordinate;
            }
        }

        //add route view in the map
        function updateRoute() {
            routeQuery.clearWaypoints();
            routeQuery.addWaypoint(startMarker.coordinate);
            routeQuery.addWaypoint(endMarker.coordinate);
            map.addMapItem(startMarker)
            map.addMapItem(endMarker)
        }

        //clear route view in the map
        function clearRoute() {
            routeQuery.clearWaypoints();
            routeModel.reset();
            map.removeMapItem(startMarker)
            map.removeMapItem(endMarker)
        }

        CheapRuler {
            id: ruler

            onCurrentDistanceChanged: {
                var total = 0;
                var i = 0;
                var alldistance = ruler.distance * 1000;

                if((routeModel.status === RouteModel.Ready)
                && (routeModel.count === 1))
                {
                    // XXX: Use car speed in meters to pre-warn the turn instruction
                    while (total < ruler.currentDistance && i < routeModel.get(0).segments.length)
                    {
                        total += routeModel.get(0).segments[i++].maneuver.distanceToNextInstruction;
                    }

                    //show the tbt board(it will be always show when demo start)
                    tbt_board.visible = true

                     // Set turn instruction
                    tbt_board.do_setTurnInstructions(routeModel.get(0).segments[i].maneuver.instructionText)
                    tbt_board.state = routeModel.get(0).segments[i].maneuver.direction

                    //when goto the last instruction,set the states to "arriveDest"
                    if(i >= (routeModel.get(0).segments.length-1))
                    {
                        total = alldistance;
                        tbt_board.state = "arriveDest";
                    }

                    var dis = (total - ruler.currentDistance).toFixed(1);

                    // Set distance
                    tbt_board.do_setDistance(dis)

                     // Set board status
                    if(dis < mapWindow.disOffset && i < routeModel.get(0).segments.length)
                    {
                        //show the tbt board(the big one)
                        tbt_board.do_showTbtboard(true)
                     }
                    else
                    {
                        //disvisible the tbt board(the big one)
                        tbt_board.do_showTbtboard(false)
                    }
                }
            }
        }
    }

    //the route view display by RouteModel
    RouteModel {
        id: routeModel

        autoUpdate: true
        query: routeQuery

        plugin: Plugin {
            name: "mapbox"

            // Development access token, do not use in production.
            PluginParameter {
                name: "mapbox.access_token"
                value: fileOperation.getMapAccessToken()
            }
        }
    }

    RouteQuery {
        id: routeQuery
    }

    Component.onCompleted: {
        //request the route info when map load finish
        if (ruler) {
            ruler.initRouteInfo();
            ruler.setCurrentPosition(fileOperation.getStartLatitude(), fileOperation.getStartLongitute());
        }
    }

    //the functions can be called by outside
    //add route signal function
    function do_addRoutePoint(poi_Lat_s, poi_Lon_s, poi_Lat_e, poi_Lon_e) {
        //set the startPoint and endPoint
        startPoint= QtPositioning.coordinate(poi_Lat_s,poi_Lon_s);
        endPoint = QtPositioning.coordinate(poi_Lat_e,poi_Lon_e);
        startMarker.coordinate = startPoint;
        endMarker.coordinate = endPoint;
        //update the route view
        if (map) {
            map.updateRoute();
        }
    }

    //set the current position
    function do_setCoordinate(latitude,longitude,direction,distance) {
        ruler.setCurrentPosition(latitude, longitude);
        ruler.setCurrentDistance(distance);
    }

    //stop navidemo signal
    function do_stopnavidemo() {
        //disvisible the tbt board
        tbt_board.visible = false
        //clear the routeview
        if (map) {
            map.clearRoute();
        }
    }

    //arrvice the destination signal
    function do_arrivedest(){
        //disvisible the tbt board
        tbt_board.visible = false
    }
}