Skip to content

Add feature layer with time offset

View on GitHub

Display a time-enabled feature layer with a time offset.

Image of Add feature layer with time offset sample

Use case

You can use a time offset to compare time periods by displaying them overlaid on the map. For example, you could show a feature layer with flu cases from December overlaid with flu cases from January to visualize the spread of the disease over time.

How to use the sample

When the sample loads, you'll see hurricane tracks visualized in red and blue. The red hurricane tracks occurred 10 days before the tracks displayed in blue. Adjust the slider to move the interval to visualize how storms progress over time.

How it works

  1. Create a feature layer for displaying the non-offset features and apply symbology to it.
  2. Create a second feature layer referring to the same service. Apply a 10 day time offset and unique symbology. Features displayed from this layer will have time values 10 days earlier than the values in the non-offset layer.
  3. Apply a 10-day time extent to the map view, starting at the beginning of the data range.
  4. When the user adjusts the slider, move the time extent. Both feature layers will filter their content for the map view's time extent.

Relevant API

  • FeatureLayer
  • MapView
  • TimeExtent

About the data

The sample uses a time-enabled feature service depicting hurricanes in the year 2000.

Tags

change, range, time, time extent, time offset, time-aware, time-enabled

Sample Code

AddFeatureLayerWithTimeOffsetView.swift
Use dark colors for code blocksCopy
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
// Copyright 2025 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
//
//   https://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.

import ArcGIS
import SwiftUI

struct AddFeatureLayerWithTimeOffsetView: View {
    /// A map with an oceans basemap and hurricane layers.
    @State private var map: Map = {
        // Makes a new map with an oceans basemap style.
        let map = Map(basemapStyle: .arcGISOceans)

        // Makes the hurricanes feature layer with no offset.
        let noOffsetLayer = FeatureLayer(featureTable: ServiceFeatureTable(url: .hurricanesService))

        // Applies a blue dot renderer to distinguish hurricanes without offset.
        noOffsetLayer.renderer = SimpleRenderer(symbol: SimpleMarkerSymbol(style: .circle, color: .blue, size: 10))

        // Makes the offset hurricanes feature layer.
        let withOffsetLayer = FeatureLayer(featureTable: ServiceFeatureTable(url: .hurricanesService))

        // Applies a red dot renderer to distinguish the hurricanes with an offset.
        withOffsetLayer.renderer = SimpleRenderer(symbol: SimpleMarkerSymbol(style: .circle, color: .red, size: 10))

        // Sets the time offset on the layer.
        withOffsetLayer.timeOffset = TimeValue(duration: 10.0, unit: .days)

        // Adds the layers to the map.
        map.addOperationalLayers([noOffsetLayer, withOffsetLayer])

        // Sets the initial viewpoint of the map.
        map.initialViewpoint = Viewpoint(latitude: 45.0, longitude: -45.0, scale: 9e7)

        return map
    }()

    /// The time extent of the map view.
    @State private var timeExtent: TimeExtent? = TimeExtent(
        startDate: .august4th2000,
        endDate: Calendar.current.date(byAdding: .day, value: 10, to: .august4th2000)
    )

    /// The value of the slider.
    @State private var sliderValue = 0.0

    /// The maximum value of the slider.
    private let sliderMaxValue = Double(
        Calendar.current.dateComponents([.day], from: Date.august4th2000, to: Date.october22nd2000).day!
    )

    /// The format style used to display the extent dates.
    private let dateFormat = Date.FormatStyle(date: .numeric, time: .omitted)

    var body: some View {
        MapView(map: map, timeExtent: $timeExtent)
            .overlay(alignment: .topTrailing) {
                VStack(alignment: .leading) {
                    Text("Hurricane Data Offset:")
                    LabeledContent {
                        Text("10 days")
                    } label: {
                        Text("Red").foregroundStyle(.red)
                    }
                    LabeledContent {
                        Text("No offset")
                    } label: {
                        Text("Blue").foregroundStyle(.blue)
                    }
                }
                .fixedSize()
                .padding()
                .background(.thinMaterial)
                .clipShape(.rect(cornerRadius: 10))
                .shadow(radius: 50)
                .padding()
            }
            .toolbar {
                ToolbarItem(placement: .bottomBar) {
                    VStack {
                        Slider(value: $sliderValue, in: 0...sliderMaxValue, step: 1)
                            .padding(.horizontal)
                            .onChange(of: sliderValue) {
                                // Calculates a new time extent when the slider value changes.
                                guard let newStartDate = Calendar.current.date(
                                        byAdding: .day,
                                        value: Int(sliderValue),
                                        to: .august4th2000
                                      ),
                                      let newEndDate = Calendar.current.date(
                                        byAdding: .day,
                                        value: 10,
                                        to: newStartDate
                                      ) else {
                                    return
                                }
                                timeExtent = TimeExtent(startDate: newStartDate, endDate: newEndDate)
                            }
                        Text(
                            """
                            \(timeExtent?.startDate?.formatted(dateFormat) ?? "") - \
                            \(timeExtent?.endDate?.formatted(dateFormat) ?? "")
                            """
                        )
                    }
                }
            }
    }
}

private extension URL {
    static let hurricanesService = URL(
        string: "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Hurricanes/MapServer/0"
    )!
}

private extension Date {
    static let august4th2000 = Calendar.current.date(from: DateComponents(year: 2000, month: 8, day: 4))!
    static let october22nd2000 = Calendar.current.date(from: DateComponents(year: 2000, month: 10, day: 22))!
}

#Preview {
    AddFeatureLayerWithTimeOffsetView()
}

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