Show location history

View on GitHub
Sample viewer app

Display your location history on the map.

Image of show location history

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. For example, an unmapped trail could be added to the map using this technique.

How to use the sample

The sample loads with a moving simulated location data source. Click the button to start tracking the location, which will appear as red points on the map. A green line will connect the points for easier visualization. Click the button again to stop updating the location history.

How it works

  1. Create a GraphicsOverlay to show each point and another GraphicsOverlay for displaying the route line.
  2. Create a SimulatedLocationDataSource and call its setLocations() method, passing the route Polyline and new SimulationParameters as parameters. Start the SimulatedLocationDataSource to begin receiving location updates.
  3. Use a LocationChangedListener on the simulatedLocationDataSource to get location updates.
  4. When the location updates store that location, display a point on the map at the location, and re-create the route polyline.

Relevant API

  • LocationDataSource.LocationChangedEvent
  • LocationDataSource.LocationChangedListener
  • LocationDisplay.AutoPanMode
  • MapView.LocationDisplay
  • SimulatedLocationDataSource
  • SimulationParameters

About the data

A custom set of points (provided in JSON format) is used to create a Polyline and configure a SimulatedLocationDataSource. The simulated location data source enables easier testing and allows the sample to be used on devices without an actively updating GPS signal. To track a user's real position, use NMEALocationDataSource instead.

Tags

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

Sample Code

ShowLocationHistorySample.java
                                                                                                                                                                                                                                            
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
/*
 * 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.
 *
 */

package com.esri.samples.show_location_history;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Calendar;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.ToggleButton;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.CornerRadii;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;

import com.esri.arcgisruntime.ArcGISRuntimeEnvironment;
import com.esri.arcgisruntime.geometry.Geometry;
import com.esri.arcgisruntime.geometry.Point;
import com.esri.arcgisruntime.geometry.Polyline;
import com.esri.arcgisruntime.geometry.PolylineBuilder;
import com.esri.arcgisruntime.geometry.SpatialReferences;
import com.esri.arcgisruntime.loadable.LoadStatus;
import com.esri.arcgisruntime.location.LocationDataSource;
import com.esri.arcgisruntime.location.LocationDataSource.LocationChangedListener;
import com.esri.arcgisruntime.location.SimulatedLocationDataSource;
import com.esri.arcgisruntime.location.SimulationParameters;
import com.esri.arcgisruntime.mapping.ArcGISMap;
import com.esri.arcgisruntime.mapping.BasemapStyle;
import com.esri.arcgisruntime.mapping.Viewpoint;
import com.esri.arcgisruntime.mapping.view.Graphic;
import com.esri.arcgisruntime.mapping.view.GraphicsOverlay;
import com.esri.arcgisruntime.mapping.view.LocationDisplay;
import com.esri.arcgisruntime.mapping.view.MapView;
import com.esri.arcgisruntime.symbology.SimpleLineSymbol;
import com.esri.arcgisruntime.symbology.SimpleMarkerSymbol;
import com.esri.arcgisruntime.symbology.SimpleRenderer;

import org.apache.commons.io.IOUtils;

public class ShowLocationHistorySample extends Application {

  private MapView mapView;
  private Point position;

  @Override
  public void start(Stage stage) {

    try {

      // create stack pane and application scene
      StackPane stackPane = new StackPane();
      Scene scene = new Scene(stackPane);

      // set title, size, and add scene to stage
      stage.setTitle("Show Location History Sample");
      stage.setWidth(800);
      stage.setHeight(700);
      stage.setScene(scene);
      stage.show();
      scene.getStylesheets().add(getClass().getResource("/show_location_history/style.css").toExternalForm());

      // authentication with an API key or named user is required to access basemaps and other location services
      String yourAPIKey = System.getProperty("apiKey");
      ArcGISRuntimeEnvironment.setApiKey(yourAPIKey);

      // create a map with the dark gray basemap style
      ArcGISMap map = new ArcGISMap(BasemapStyle.ARCGIS_DARK_GRAY);

      // create a map view and set the map to it
      mapView = new MapView();
      mapView.setMap(map);

      // set a viewpoint on the map view centered on Los Angeles, California
      mapView.setViewpoint(new Viewpoint(new Point(-13185535.98, 4037766.28, SpatialReferences.getWebMercator()), 7000));

      // create a label
      Label trackingLabel = new Label("Tracking Status: ");

      // create a button that toggles the location tracking
      ToggleButton trackingButton = new ToggleButton("Start");
      trackingButton.maxWidth(Double.MAX_VALUE);
      trackingButton.setDisable(true);

      // create a graphics overlay for the points and use a red circle for the symbols
      GraphicsOverlay locationHistoryOverlay = new GraphicsOverlay();
      SimpleMarkerSymbol locationSymbol = new SimpleMarkerSymbol(
        SimpleMarkerSymbol.Style.CIRCLE, 0xFFFF0000, 10f);
      SimpleRenderer locationHistoryRenderer = new SimpleRenderer(locationSymbol);
      locationHistoryOverlay.setRenderer(locationHistoryRenderer);

      // create a graphics overlay for the lines connecting the points and use a green line for the symbol
      GraphicsOverlay locationHistoryLineOverlay = new GraphicsOverlay();
      SimpleLineSymbol locationLineSymbol = new SimpleLineSymbol(
        SimpleLineSymbol.Style.SOLID, 0xFF00FF00, 2.0f);
      SimpleRenderer locationHistoryLineRenderer = new SimpleRenderer(locationLineSymbol);
      locationHistoryLineOverlay.setRenderer(locationHistoryLineRenderer);

      // add the graphics overlays to the map view
      mapView.getGraphicsOverlays().addAll(Arrays.asList(locationHistoryOverlay, locationHistoryLineOverlay));

      // create a polyline builder to connect the location points
      PolylineBuilder polylineBuilder = new PolylineBuilder(SpatialReferences.getWebMercator());

      // access the json of the location points
      String polylineData = IOUtils.toString(getClass().getResourceAsStream("/show_location_history/polyline_data.json"), StandardCharsets.UTF_8);
      // create a polyline from the location points
      Polyline routePolyline = (Polyline) Geometry.fromJson(polylineData, SpatialReferences.getWebMercator());

      // create a simulated location data source
      SimulatedLocationDataSource simulatedLocationDataSource = new SimulatedLocationDataSource();
      // set the location of the simulated location data source with simulation parameters to set a consistent velocity
      simulatedLocationDataSource.setLocations(
        routePolyline, new SimulationParameters(Calendar.getInstance(), 30.0, 0.0, 0.0));

      // configure the map view's location display to follow the simulated location data source
      LocationDisplay locationDisplay = mapView.getLocationDisplay();
      locationDisplay.setLocationDataSource(simulatedLocationDataSource);
      locationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER);
      locationDisplay.setInitialZoomScale(7000);

      // create a listener to track the previous location, to draw a route line behind the location display symbol
      LocationChangedListener locationChangedListener =
        (LocationDataSource.LocationChangedEvent locationChangedEvent) -> {

        // reset the old polyline connecting the points
        locationHistoryLineOverlay.getGraphics().clear();

        // add any previous position to the history
        if (position != null) {
          // add the new point to the polyline
          polylineBuilder.addPoint(position);
          // add the new point to the graphics overlay
          locationHistoryOverlay.getGraphics().add(new Graphic(position));
        }
        // store the current position
        position = locationChangedEvent.getLocation().getPosition();

        // add the updated polyline to the graphics overlay
        locationHistoryLineOverlay.getGraphics().add(new Graphic(polylineBuilder.toGeometry()));
      };

      trackingButton.setOnAction(event -> {
        // if the user has panned away from the location display, turn it on again
        if (locationDisplay.getAutoPanMode() == LocationDisplay.AutoPanMode.OFF) {
          locationDisplay.setAutoPanMode(LocationDisplay.AutoPanMode.RECENTER);
        }
        // toggle the location tracking when the button is clicked
        if (trackingButton.isSelected()) {
          simulatedLocationDataSource.addLocationChangedListener(locationChangedListener);
          trackingButton.setText("Active");
          trackingButton.setStyle("-fx-focus-color: #00ff00;");
        } else {
          simulatedLocationDataSource.removeLocationChangedListener(locationChangedListener);
          trackingButton.setText("Stopped");
          trackingButton.setStyle("-fx-focus-color: #f16345;");
        }
      });

      // enable the button interactions when the map is loaded
      map.addDoneLoadingListener(() -> {

        if (map.getLoadStatus() == LoadStatus.LOADED) {
          trackingButton.setDisable(false);

          // start the simulated location data source
          simulatedLocationDataSource.startAsync();

        } else {
          new Alert(Alert.AlertType.ERROR, "Map failed to load: " + map.getLoadError().getCause().getMessage()).show();
        }
      });

      // create a control panel and add the label and button UI components
      HBox controlsHBox = new HBox();
      controlsHBox.setBackground(new Background(new BackgroundFill(Paint.valueOf("rgba(0,0,0,0.3)"), CornerRadii.EMPTY, Insets.EMPTY)));
      controlsHBox.setPadding(new Insets(10.0));
      controlsHBox.setAlignment(Pos.CENTER);
      controlsHBox.setMaxSize(210, 50);
      controlsHBox.getStyleClass().add("panel-region");
      controlsHBox.getChildren().addAll(trackingLabel, trackingButton);

      // add the map view and control panel to the stack pane
      stackPane.getChildren().addAll(mapView, controlsHBox);
      StackPane.setAlignment(controlsHBox, Pos.TOP_LEFT);
      StackPane.setMargin(controlsHBox, new Insets(10, 0, 0, 10));

    } catch (Exception e) {
      // on any error, display the stack trace.
      e.printStackTrace();
    }
  }

  /**
   * Stops and releases all resources used in application.
   */
  @Override
  public void stop() {

    if (mapView != null) {
      mapView.dispose();
    }
  }

  /**
   * Opens and runs application.
   *
   * @param args arguments passed to this application
   */
  public static void main(String[] args) {

    Application.launch(args);
  }
}

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