1function getLocationData() { 2 /* 3 const latitude = getCookie('latitude'); 4 const longitude = getCookie('longitude'); 5 const state = getCookie('state'); 6 7 if (latitude && longitude && state) { 8 console.log('Usando dados do cookie:', latitude, longitude, state); 9 // Faça algo com os valores dos cookies, se necessário 10 } else {*/ 11 if (navigator.geolocation) { 12 navigator.geolocation.getCurrentPosition(showPosition, showError); 13 } else { 14 alert("Geolocalização não é suportada por este navegador."); 15 } 16 //} 17} 18 19function showPosition(position) { 20 const latitude = position.coords.latitude; 21 const longitude = position.coords.longitude; 22 23 fetch('/wp-json/geolocation-plugin/v1/get-api') 24 .then(response => response.json()) 25 .then(data => { 26 const api = data.api; 27 const apiKey = data.api_key; 28 let url = ''; 29 30 if (api === 'opencage') { 31 url = `https://api.opencagedata.com/geocode/v1/json?q=${latitude}+${longitude}&key=${apiKey}`; 32 } else if (api === 'mapmaker') { 33 url = `https://geocode.maps.co/reverse?lat=${latitude}&lon=${longitude}&api_key=${apiKey}`; 34 } else if (api === 'positionstack') { 35 url = `http://api.positionstack.com/v1/reverse?access_key=${apiKey}&query=${latitude},${longitude}`; 36 } else if (api === 'googlemaps') { 37 url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${latitude},${longitude}&key=${apiKey}`; 38 } else if (api === 'heremaps') { 39 url = `https://revgeocode.search.hereapi.com/v1/revgeocode?apikey=${apiKey}&at=${latitude},${longitude}&lang=pt-BR`; 40 } 41 42 fetch(url) 43 .then(response => response.json()) 44 .then(data => { 45 let state = ''; 46 if (api === 'opencage' && data.results && data.results.length > 0) { 47 state = data.results[0].components.state || data.results[0].components.region; 48 } else if (api === 'mapmaker' && data.address) { 49 state = data.address.state || data.address.region; 50 } else if (api === 'positionstack' && data.data && data.data.length > 0) { 51 state = data.data[0].region; 52 } else if (api === 'heremaps' && data.items && data.items.length > 0) { 53 state = data.items[0].address.state; 54 } else if (api === 'googlemaps' && data.results && data.results.length > 0) { 55 const addressComponents = data.results[0].address_components; 56 for (let i = 0; i < addressComponents.length; i++) { 57 if (addressComponents[i].types.includes('administrative_area_level_1')) { 58 state = addressComponents[i].long_name; 59 break; 60 } 61 } 62 } 63 64 setCookie('latitude', latitude, 7); 65 setCookie('longitude', longitude, 7); 66 setCookie('state', state, 7); 67 setCookie('status', 'gps', 7); 68 refresh2(); 69 }) 70 .catch(error => console.error('Erro ao buscar dados de geocodificação: ', error)); 71 }); 72} 73 74function refresh2() { 75 setTimeout(function () { 76 location.reload() 77 }, 100); 78} 79 80function showError(error) { 81 switch(error.code) { 82 case error.PERMISSION_DENIED: 83 console.log("Usuário negou a solicitação de Geolocalização."); 84 break; 85 case error.POSITION_UNAVAILABLE: 86 console.log("Informação de localização não está disponÃvel."); 87 break; 88 case error.TIMEOUT: 89 console.log("A solicitação para obter a localização expirou."); 90 break; 91 case error.UNKNOWN_ERROR: 92 console.log("Ocorreu um erro desconhecido."); 93 break; 94 } 95} 96 97function setCookie(name, value, days) { 98 const date = new Date(); 99 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 100 const expires = "expires=" + date.toUTCString(); 101 document.cookie = name + "=" + value + ";" + expires + ";path=/"; 102} 103 104function getCookie(name) { 105 const value = `; ${document.cookie}`; 106 const parts = value.split(`; ${name}=`); 107 if (parts.length === 2) return parts.pop().split(';').shift(); 108} 109 110// Exemplo de uso no HTML: 111// <button onclick="getLocationData()">Obter Localização</button>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.