-
Notifications
You must be signed in to change notification settings - Fork 0
/
places.js
128 lines (107 loc) · 4.02 KB
/
places.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
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
window.onload = () => {
let method = 'dynamic';
// if you want to statically add places, de-comment following line
method = 'static';
if (method === 'static') {
let places = staticLoadPlaces();
renderPlaces(places);
}
if (method !== 'static') {
// first get current user location
return navigator.geolocation.getCurrentPosition(function (position) {
// than use it to load from remote APIs some places nearby
dynamicLoadPlaces(position.coords)
.then((places) => {
renderPlaces(places);
})
},
(err) => console.error('Error in retrieving position', err),
{
enableHighAccuracy: true,
maximumAge: 0,
timeout: 27000,
}
);
}
};
function staticLoadPlaces() {
return [
{
name: "Casa Maurício",
location: {
lat: 41.495982, // add here latitude if using static data
lng: -8.336398, // add here longitude if using static data
}
},
{
name: 'Casa Rolão',
location: {
lat: 41.550441,
lng: -8.4210637,
}
}
];
}
// getting places from REST APIs
function dynamicLoadPlaces(position) {
let params = {
radius: 300, // search places not farther than this value (in meters)
clientId: 'HZIJGI4COHQ4AI45QXKCDFJWFJ1SFHYDFCCWKPIJDWHLVQVZ', // add your credentials here
clientSecret: '', // add your credentials here
version: '20300101', // foursquare versioning, required but unuseful for this demo
};
// CORS Proxy to avoid CORS problems
let corsProxy = 'https://cors-anywhere.herokuapp.com/';
// Foursquare API
let endpoint = `${corsProxy}https://api.foursquare.com/v2/venues/search?intent=checkin
&ll=${position.latitude},${position.longitude}
&radius=${params.radius}
&client_id=${params.clientId}
&client_secret=${params.clientSecret}
&limit=15
&v=${params.version}`;
return fetch(endpoint)
.then((res) => {
return res.json()
.then((resp) => {
return resp.response.venues;
})
})
.catch((err) => {
console.error('Error with places API', err);
})
};
function renderPlaces(places) {
let scene = document.querySelector('a-scene');
places.forEach((place) => {
const latitude = place.location.lat;
const longitude = place.location.lng;
// add place icon
const icon = document.createElement('a-image');
icon.setAttribute('gps-entity-place', `latitude: ${latitude}; longitude: ${longitude}`);
icon.setAttribute('name', place.name);
icon.setAttribute('src', 'map-marker.png');
// for debug purposes, just show in a bigger scale, otherwise I have to personally go on places...
icon.setAttribute('scale', '20, 20');
icon.addEventListener('loaded', () => window.dispatchEvent(new CustomEvent('gps-entity-place-loaded')));
const clickListener = function (ev) {
ev.stopPropagation();
ev.preventDefault();
const name = ev.target.getAttribute('name');
const el = ev.detail.intersection && ev.detail.intersection.object.el;
if (el && el === ev.target) {
const label = document.createElement('span');
const container = document.createElement('div');
container.setAttribute('id', 'place-label');
label.innerText = name;
container.appendChild(label);
document.body.appendChild(container);
setTimeout(() => {
container.parentElement.removeChild(container);
}, 1500);
}
};
icon.addEventListener('click', clickListener);
scene.appendChild(icon);
});
}