-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicUsage.swift
More file actions
106 lines (94 loc) · 3.28 KB
/
BasicUsage.swift
File metadata and controls
106 lines (94 loc) · 3.28 KB
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
import BigDataCloudClient
import SwiftUI
// MARK: - Example 1: Simple SwiftUI view
struct LocationView: View {
@State private var result: GeocodeResult?
@State private var error: String?
@State private var loading = false
private let client = BigDataCloudClient()
var body: some View {
VStack(spacing: 16) {
if loading {
ProgressView("Locating…")
} else if let result {
VStack(alignment: .leading, spacing: 8) {
Text(result.response.city)
.font(.title2.bold())
Text(result.response.countryName)
.foregroundStyle(.secondary)
// Always show accuracy level — never silently use IP-based
accuracyBadge(for: result.accuracy)
}
} else if let error {
Text(error).foregroundStyle(.red)
}
Button("Get My Location") {
Task { await locate() }
}
.buttonStyle(.borderedProminent)
}
.padding()
}
@ViewBuilder
private func accuracyBadge(for accuracy: AccuracyLevel) -> some View {
switch accuracy {
case .fine:
Label("GPS location", systemImage: "location.fill")
.font(.caption)
.foregroundStyle(.green)
case .coarse:
Label("Approximate location", systemImage: "location")
.font(.caption)
.foregroundStyle(.orange)
case .ipBased:
// Always inform the user when falling back to IP
Label("IP-based location — enable location for better accuracy", systemImage: "location.slash")
.font(.caption)
.foregroundStyle(.red)
}
}
@MainActor
private func locate() async {
loading = true
error = nil
do {
result = try await client.reverseGeocode()
} catch {
self.error = error.localizedDescription
}
loading = false
}
}
// MARK: - Example 2: Roaming detection
struct RoamingView: View {
@State private var roaming: RoamingResponse?
private let client = BigDataCloudClient()
var body: some View {
VStack {
if let roaming {
if roaming.isRoaming == true {
Text("You are roaming in \(roaming.roamingCountryName ?? "unknown")")
} else {
Text("Not roaming — home network detected")
}
}
Button("Check Roaming") {
Task {
roaming = try? await client.amIRoaming()
}
}
}
}
}
// MARK: - Fair use policy
//
// This API is governed by BigDataCloud's Fair Use Policy:
// https://www.bigdatacloud.com/docs/article/fair-use-policy-for-free-client-side-reverse-geocoding-api
//
// Only real-time coordinates from the calling device are permitted.
// Pre-stored, cached or externally-sourced coordinates are NOT allowed and
// will result in a 402 error and IP ban.
//
// Need to geocode coordinates you already have? Use the server-side API —
// it includes 50,000 free queries/month with an API key:
// https://www.bigdatacloud.com/docs/sdks