window.onload = () => {
let method = 'dynamic';
method = 'static';
if (method === 'static') {
let places = staticLoadPlaces();
renderPlaces(places);
}
if (method !== 'static') {
return navigator.geolocation.getCurrentPosition(function (position) {
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: 0,
lng: 0,
}
},
{
name: 'Another place name',
location: {
lat: 0,
lng: 0,
}
}
];
}
function dynamicLoadPlaces(position) {
let params = {
radius: 300,
clientId: 'HZIJGI4COHQ4AI45QXKCDFJWFJ1SFHYDFCCWKPIJDWHLVQVZ',
clientSecret: '',
version: '20300101',
};
let corsProxy = 'https://cors-anywhere.herokuapp.com/';
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;
const icon = document.createElement('a-image');
icon.setAttribute('gps-entity-place', `latitude: ${latitude}; longitude: ${longitude}`);
icon.setAttribute('name', place.name);
icon.setAttribute('src', '../assets/map-marker.png');
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);
});
}