Skip to content

Commit

Permalink
fix: mobile geolocation
Browse files Browse the repository at this point in the history
  • Loading branch information
pozil committed Oct 2, 2023
1 parent 8b27b96 commit 20b4b19
Showing 3 changed files with 35 additions and 33 deletions.
48 changes: 23 additions & 25 deletions force-app/main/default/lwc/propertyLocation/propertyLocation.html
Original file line number Diff line number Diff line change
@@ -9,32 +9,30 @@
errors={error}
></c-error-panel>
</template>
<template lwc:else>
<template lwc:if={distance}>
<div class="slds-var-m-around_medium location">
<div class="slds-text-heading_small">
Current latitude:
<b class="latitude">&nbsp;{location.latitude}</b>
</div>
<div class="slds-text-heading_small">
Current longitude:
<b class="longitude">&nbsp;{location.longitude}</b>
</div>
<div class="slds-text-heading_small">
Distance to property:&nbsp;<b>
<lightning-formatted-number
value={distance}
maximum-fraction-digits="2"
></lightning-formatted-number>
miles
</b>
</div>
<template lwc:elseif={distance}>
<div class="slds-var-m-around_medium location">
<div class="slds-text-heading_small">
Current latitude:
<b class="latitude">&nbsp;{location.coords.latitude}</b>
</div>
<div class="slds-text-heading_small">
Current longitude:
<b class="longitude">&nbsp;{location.coords.longitude}</b>
</div>
<div class="slds-text-heading_small">
Distance to property:&nbsp;<b>
<lightning-formatted-number
value={distance}
maximum-fraction-digits="2"
></lightning-formatted-number>
miles
</b>
</div>
</template>
<template lwc:else>
<lightning-spinner alternative-text="Loading..." size="small">
</lightning-spinner>
</template>
</div>
</template>
<template lwc:else>
<lightning-spinner alternative-text="Loading..." size="small">
</lightning-spinner>
</template>
</lightning-card>
</template>
14 changes: 8 additions & 6 deletions force-app/main/default/lwc/propertyLocation/propertyLocation.js
Original file line number Diff line number Diff line change
@@ -19,7 +19,6 @@ export default class PropertyLocation extends LightningElement {
wiredProperty({ data, error }) {
if (data) {
this.property = data;
this.error = undefined;
this.calculateDistance();
} else if (error) {
this.error = error;
@@ -42,8 +41,11 @@ export default class PropertyLocation extends LightningElement {

async calculateLocationFromMobileDevice() {
try {
this.location =
await this.deviceLocationService.getCurrentPosition();
this.location = await this.deviceLocationService.getCurrentPosition(
{
enableHighAccuracy: true
}
);
this.calculateDistance();
} catch (error) {
this.error = error;
@@ -53,7 +55,7 @@ export default class PropertyLocation extends LightningElement {
calculateLocationFromBrowser() {
navigator.geolocation.getCurrentPosition(
(result) => {
this.location = result.coords;
this.location = result;
this.calculateDistance();
},
(error) => {
@@ -64,9 +66,9 @@ export default class PropertyLocation extends LightningElement {

calculateDistance() {
if (this.location && this.property) {
const latitude1 = this.location.latitude;
const latitude1 = this.location.coords.latitude;
const latitude2 = getFieldValue(this.property, LATITUDE_FIELD);
const longitude1 = this.location.longitude;
const longitude1 = this.location.coords.longitude;
const longitude2 = getFieldValue(this.property, LONGITUDE_FIELD);

// Haversine formula
6 changes: 4 additions & 2 deletions force-app/test/jest-mocks/lightning/mobileCapabilities.js
Original file line number Diff line number Diff line change
@@ -11,8 +11,10 @@ export const getLocationService = jest.fn().mockImplementation(() => {
isAvailable: jest.fn().mockReturnValue(_deviceLocationServiceAvailable),
getCurrentPosition: jest.fn().mockImplementation(() =>
Promise.resolve({
latitude: 42.361145,
longitude: -71.057083
coords: {
latitude: 42.361145,
longitude: -71.057083
}
})
)
};

0 comments on commit 20b4b19

Please sign in to comment.