-
Notifications
You must be signed in to change notification settings - Fork 3
/
boatsNearMe.js
79 lines (72 loc) · 2.44 KB
/
boatsNearMe.js
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
import { api, LightningElement, wire } from 'lwc';
import getBoatsByLocation from '@salesforce/apex/BoatDataService.getBoatsByLocation';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
const LABEL_YOU_ARE_HERE = 'You are here!';
const ICON_STANDARD_USER = 'standard:user';
const ERROR_TITLE = 'Error loading Boats Near Me';
const ERROR_VARIANT = 'error';
export default class BoatsNearMe extends LightningElement {
@api
boatTypeId;
mapMarkers = [];
isLoading = true;
isRendered;
latitude;
longitude;
// Add the wired method from the Apex Class
// Name it getBoatsByLocation, and use latitude, longitude and boatTypeId
// Handle the result and calls createMapMarkers
@wire(getBoatsByLocation, {latitude: '$latitude', longitude: '$longitude', boatTypeId: '$boatTypeId'})
wiredBoatsJSON({error, data}) {
if (data) {
this.createMapMarkers(data);
} else if (error) {
const toast = new ShowToastEvent({
title: ERROR_TITLE,
message: error.message,
variant: ERROR_VARIANT,
});
this.dispatchEvent(toast);
}
this.isLoading = false;
}
// Controls the isRendered property
// Calls getLocationFromBrowser()
renderedCallback() {
if (!this.isRendered) {
this.getLocationFromBrowser();
}
this.isRendered = true;
}
// Gets the location from the Browser
// position => {latitude and longitude}
getLocationFromBrowser() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(position => {
this.latitude = position.coords.latitude;
this.longitude = position.coords.longitude;
});
}
}
// Creates the map markers
createMapMarkers(boatData) {
const newMarkers = JSON.parse(boatData).map(boat => {
return {
title: boat.Name,
location: {
Latitude: boat.Geolocation__Latitude__s,
Longitude: boat.Geolocation__Longitude__s
}
};
});
newMarkers.unshift({
title: LABEL_YOU_ARE_HERE,
icon: ICON_STANDARD_USER,
location: {
Latitude: this.latitude,
Longitude: this.longitude
}
});
this.mapMarkers = newMarkers;
}
}