index.js (834B)
1 import config from '~/config'; 2 import {fetch} from '~/lib/xhr-promise'; 3 4 class ElevationProvider { 5 chunkSize = 10000; 6 7 constructor(serverUrl = config.elevationsServer) { 8 this.url = serverUrl; 9 } 10 11 async get(latlngs) { 12 const result = []; 13 for (let i = 0; i < latlngs.length; i += this.chunkSize) { 14 const chunk = latlngs.slice(i, i + this.chunkSize); 15 const request = chunk.map((ll) => `${ll.lat.toFixed(6)} ${ll.lng.toFixed(6)}`).join('\n'); 16 const xhr = await fetch(this.url, {method: 'POST', data: request, withCredentials: true}); 17 const respValues = xhr.responseText.split('\n').map((line) => (line === 'NULL' ? null : parseFloat(line))); 18 result.push(...respValues); 19 } 20 return result; 21 } 22 } 23 24 export {ElevationProvider};