Skip to content

Commit b518088

Browse files
committed
remove heading from getCurrentPosition
1 parent bb812ed commit b518088

5 files changed

Lines changed: 40 additions & 19 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
### Features
44

5-
* **heading:** Added support for accurate heading information across all platforms.
6-
* **heading:** The `heading` property on iOS, Android, and Web now prioritizes actual compass bearing (true/magnetic heading) when available, falling back to direction of travel (course).
5+
* **heading:** Added support for accurate heading information across all platforms (available for `watchPosition` only).
6+
* **heading:** The `heading` property on iOS, Android, and Web now prioritizes actual compass bearing (true/magnetic heading) when available during active watches, falling back to direction of travel (course).
77
* **coords:** Added `magneticHeading`, `trueHeading`, `headingAccuracy`, and `course` to `Position.coords`.
88
* **web:** Improved heading support using `DeviceOrientation` APIs.
99

android/src/main/kotlin/com/capacitorjs/plugins/geolocation/GeolocationPlugin.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ class GeolocationPlugin : Plugin() {
254254
put("altitude", locationResult.altitude)
255255
locationResult.altitudeAccuracy?.let { put("altitudeAccuracy", it) }
256256
put("speed", locationResult.speed)
257-
put("heading", locationResult.heading)
258-
locationResult.magneticHeading?.let { put("magneticHeading", it) }
259-
locationResult.trueHeading?.let { put("trueHeading", it) }
260-
locationResult.headingAccuracy?.let { put("headingAccuracy", it) }
261-
locationResult.course?.let { put("course", it) }
257+
put("heading", if (locationResult.heading != -1f) locationResult.heading else null)
258+
put("magneticHeading", locationResult.magneticHeading)
259+
put("trueHeading", locationResult.trueHeading)
260+
put("headingAccuracy", locationResult.headingAccuracy)
261+
put("course", locationResult.course)
262262
}
263263
return JSObject().apply {
264264
put("timestamp", locationResult.timestamp)

example-app/src/js/capacitor-welcome.js

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,26 @@ window.customElements.define(
207207
}
208208
const timeRepresentation = location.timestamp ? new Date(location.timestamp).toISOString() : '-';
209209
stringRepresentation += `- Time: ${timeRepresentation}\n`;
210-
stringRepresentation += `- Latitute: ${location?.coords.latitude}\n- Longitude: ${location?.coords.longitude}\n`;
211-
if (location?.coords.altitude || location?.coords.heading || location?.coords.speed) {
212-
stringRepresentation += `- Altitude: ${location?.coords.altitude}\n- Heading: ${location?.coords.heading}\n- Speed: ${location?.coords.speed}\n`;
213-
}
210+
stringRepresentation += `- Latitude: ${location?.coords.latitude}\n- Longitude: ${location?.coords.longitude}\n`;
214211
stringRepresentation += `- Accuracy: ${location?.coords.accuracy}\n`;
215-
if (location?.coords.altitudeAccuracy) {
216-
stringRepresentation += `- Altitude accuracy: ${location?.coords.altitudeAccuracy}\n`;
212+
stringRepresentation += `- Altitude: ${location?.coords.altitude}\n`;
213+
stringRepresentation += `- Altitude accuracy: ${location?.coords.altitudeAccuracy}\n`;
214+
stringRepresentation += `- Speed: ${location?.coords.speed}\n`;
215+
216+
if (location?.coords.heading !== null && location?.coords.heading !== undefined && location?.coords.heading !== -1) {
217+
stringRepresentation += `- Heading: ${location.coords.heading}\n`;
218+
}
219+
if (location?.coords.magneticHeading !== null && location?.coords.magneticHeading !== undefined && location?.coords.magneticHeading !== -1) {
220+
stringRepresentation += `- Magnetic Heading: ${location.coords.magneticHeading}\n`;
221+
}
222+
if (location?.coords.trueHeading !== null && location?.coords.trueHeading !== undefined && location?.coords.trueHeading !== -1) {
223+
stringRepresentation += `- True Heading: ${location.coords.trueHeading}\n`;
224+
}
225+
if (location?.coords.headingAccuracy !== null && location?.coords.headingAccuracy !== undefined && location?.coords.headingAccuracy !== -1) {
226+
stringRepresentation += `- Heading Accuracy: ${location.coords.headingAccuracy}\n`;
227+
}
228+
if (location?.coords.course !== null && location?.coords.course !== undefined && location?.coords.course !== -1) {
229+
stringRepresentation += `- Course: ${location.coords.course}\n`;
217230
}
218231
return stringRepresentation;
219232
}

src/definitions.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,35 @@ export interface Position {
153153
/**
154154
* The heading (measured in degrees) relative to magnetic north.
155155
*
156+
* Only available when using `watchPosition`.
157+
*
156158
* @since 8.1.0
157159
*/
158160
magneticHeading: number | null | undefined;
159161

160162
/**
161163
* The heading (measured in degrees) relative to true north.
162164
*
165+
* Only available when using `watchPosition`.
166+
*
163167
* @since 8.1.0
164168
*/
165169
trueHeading: number | null | undefined;
166170

167171
/**
168172
* The maximum deviation (measured in degrees) between the reported heading and the true geomagnetic heading.
169173
*
174+
* Only available when using `watchPosition`.
175+
*
170176
* @since 8.1.0
171177
*/
172178
headingAccuracy: number | null | undefined;
173179

174180
/**
175181
* The direction in which the device is travelling, measured in degrees and relative to due north.
176182
*
183+
* Only available when using `watchPosition`.
184+
*
177185
* @since 8.1.0
178186
*/
179187
course: number | null | undefined;

src/web.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,11 @@ export class GeolocationWeb extends WebPlugin implements GeolocationPlugin {
5353
}
5454
}
5555

56-
private augmentPosition(pos: globalThis.GeolocationPosition): Position {
56+
private augmentPosition(pos: globalThis.GeolocationPosition, isWatch = false): Position {
5757
const coords = pos.coords;
58-
const orientation = this.latestOrientation;
58+
const orientation = isWatch ? this.latestOrientation : null;
5959

60-
const heading = orientation?.trueHeading ?? orientation?.magneticHeading ?? coords.heading ?? null;
60+
const heading = orientation?.trueHeading ?? orientation?.magneticHeading ?? (isWatch ? coords.heading : null) ?? null;
6161

6262
return {
6363
timestamp: pos.timestamp,
@@ -72,7 +72,7 @@ export class GeolocationWeb extends WebPlugin implements GeolocationPlugin {
7272
magneticHeading: orientation?.magneticHeading ?? null,
7373
trueHeading: orientation?.trueHeading ?? null,
7474
headingAccuracy: orientation?.headingAccuracy ?? null,
75-
course: coords.heading ?? null,
75+
course: (isWatch ? coords.heading : null) ?? null,
7676
},
7777
};
7878
}
@@ -81,7 +81,7 @@ export class GeolocationWeb extends WebPlugin implements GeolocationPlugin {
8181
return new Promise((resolve, reject) => {
8282
navigator.geolocation.getCurrentPosition(
8383
pos => {
84-
resolve(this.augmentPosition(pos));
84+
resolve(this.augmentPosition(pos, false));
8585
},
8686
err => {
8787
reject(err);
@@ -99,7 +99,7 @@ export class GeolocationWeb extends WebPlugin implements GeolocationPlugin {
9999
async watchPosition(options: PositionOptions, callback: WatchPositionCallback): Promise<CallbackID> {
100100
const id = navigator.geolocation.watchPosition(
101101
pos => {
102-
callback(this.augmentPosition(pos));
102+
callback(this.augmentPosition(pos, true));
103103
},
104104
err => {
105105
callback(null, err);

0 commit comments

Comments
 (0)