D7net
Home
Console
Upload
information
Create File
Create Folder
About
Tools
:
/
var
/
www
/
html
/
crsys
/
Filename :
pega_distancia.php
back
Copy
<!DOCTYPE html> <html lang="pt-BR"> <head> <meta charset="UTF-8"> <title>Distância entre CEPs</title> <style> body { font-family: Arial; margin: 40px; } label, input { display: block; margin-top: 10px; } input { padding: 6px; width: 200px; } button { margin-top: 20px; padding: 8px 16px; font-size: 16px; cursor: pointer; } #resultado { margin-top: 20px; font-weight: bold; color: #2c3e50; } </style> </head> <body> <h2>Calcular distância entre dois CEPs</h2> <label for="cep1">CEP Origem:</label> <input type="text" id="cep1" placeholder="Ex: 01001-000"> <label for="cep2">CEP Destino:</label> <input type="text" id="cep2" placeholder="Ex: 20040-002"> <button onclick="consultarDistancia()">Calcular Distância</button> <div id="resultado"></div> <script> async function consultarCep(cep) { const response = await fetch(`https://seusite.com/distancia.php?cep=${cep}`); return await response.json(); } async function consultarDistancia() { const cep1 = document.getElementById("cep1").value.trim(); const cep2 = document.getElementById("cep2").value.trim(); if (!cep1 || !cep2) { document.getElementById("resultado").textContent = "Preencha os dois CEPs."; return; } try { const origem = await consultarCep(cep1); const destino = await consultarCep(cep2); if (origem.lat && origem.lng && destino.lat && destino.lng) { const distancia = haversine(+origem.lat, +origem.lng, +destino.lat, +destino.lng); document.getElementById("resultado").textContent = `Distância aproximada: ${distancia.toFixed(2)} km`; } else { document.getElementById("resultado").textContent = "Não foi possível obter coordenadas dos CEPs."; } } catch (error) { document.getElementById("resultado").textContent = "Erro ao consultar os CEPs."; console.error(error); } } function haversine(lat1, lon1, lat2, lon2) { const R = 6371; const dLat = (lat2 - lat1) * Math.PI / 180; const dLon = (lon2 - lon1) * Math.PI / 180; const a = Math.sin(dLat / 2) ** 2 + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) ** 2; const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; } </script> </body> </html>