forked from jeromeetienne/AR.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaces.js
96 lines (83 loc) · 2.92 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
window.onload = () => {
let method = 'dynamic';
// if you want to statically add places, de-comment following line:
method = 'static';
if (method === 'static') {
let places = staticLoadPlaces();
return 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: "Your place name",
location: {
lat: 44.493271, // change here latitude if using static data
lng: 11.326040, // change here longitude if using static data
}
},
];
}
// getting places from REST APIs
function dynamicLoadPlaces(position) {
let params = {
radius: 300, // search places not farther than this value (in meters)
clientId: 'HZIJGI4COHQ4AI45QXKCDFJWFJ1SFHYDFCCWKPIJDWHLVQVZ',
clientSecret: '',
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) => {
let latitude = place.location.lat;
let longitude = place.location.lng;
// add place name
let text = document.createElement('a-link');
text.setAttribute('gps-entity-place', `latitude: ${latitude}; longitude: ${longitude};`);
text.setAttribute('title', place.name);
text.setAttribute('href', 'http://www.example.com/');
text.setAttribute('scale', '15 15 15');
text.addEventListener('loaded', () => {
window.dispatchEvent(new CustomEvent('gps-entity-place-loaded'))
});
scene.appendChild(text);
});
}