Show location history

View inQMLC++
View on GitHub
Sample viewer app

Display your location history on the map.

screenshot

Use case

You can track device location history and display it as lines and points on the map. The history can be used to visualize how the user moved through the world, to retrace their steps, or to create new feature geometry. An unmapped trail, for example, could be added to the map using this technique.

How to use the sample

Click 'Start tracking' to start tracking your location, which will appear as points on the map. A line will connect the points for easier visualization. Click 'Stop tracking' to stop updating the location history. This sample uses a simulated data source to allow the sample to be useful on desktop/non-mobile devices. To track a user's real position, use the DefaultLocationDataSource instead.

How it works

  1. If necessary, request location permission from the operating system.
  2. Create a graphics overlay to show each point and another graphics overlay for displaying the route line.
  3. Create a SimulatedLocationDataSource and initialize it with a polyline. Start the SimulatedLocationDataSource to begin receiving location updates.

    • NOTE: To track a user's real position, use DefaultLocationDataSource instead.
  4. Connect to the LocationDisplay's LocationChanged signal to handle location updates.
  5. Every time the location updates, store that location, display a point on the map, and re-create the route line.

Relevant API

  • Location::position
  • LocationDisplay::autoPanMode
  • LocationDisplay::dataSource
  • LocationDisplay::locationChanged
  • MapView::locationDisplay
  • SimulatedLocationDataSource

About the data

A custom set of points is used to create a Polyline and initialize a SimulatedLocationDataSource. This simulated location data source enables easier testing and allows the sample to be used on devices without an actively updating GPS signal.

Tags

bread crumb, breadcrumb, GPS, history, movement, navigation, real-time, trace, track, trail

Sample Code

ShowLocationHistory.qmlShowLocationHistory.cppShowLocationHistory.h
                                          
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
// [WriteFile Name=ShowLocationHistory, Category=Maps]
// [Legal]
// Copyright 2020 Esri.

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// [Legal]

import QtQuick 2.6
import QtQuick.Controls 2.2
import Esri.Samples 1.0

Item {

    // add a mapView component
    MapView {
        id: view
        anchors.fill: parent

        Button {
            id: button
            text: model.trackingEnabled ? "Stop tracking" : "Start tracking"
            anchors.horizontalCenter: parent.horizontalCenter
            width: 200
            onClicked: model.trackingEnabled =! model.trackingEnabled
        }
    }

    // Declare the C++ instance which creates the scene etc. and supply the view
    ShowLocationHistorySample {
        id: model
        mapView: view
    }
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.