Remover letra javascript
Pessoal, boa tarde!
Após muito procurar, consegui esse script e adaptar ao meu código, porém, eu preciso retirar um valor baseado numa KM, até aí ok, é só fazer o calculo de KM*valor
Porém o google ta me retornando "8,5km" por exemplo, e consegui transformar isso só em "85" mas preciso que fique "8.5" para a base de calculo, não sei se fui claro, mas vocês poderiam me ajudar nisso?
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Calcular distancia entre cidades (mapas e rotas)</title>
<script src="http://code.jquery.com/jquery-1.8.1.js" type="text/javascript"></script>
</head>
<body>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
function CalculaDistancia() {
$('#litResultado').html('Aguarde...');
//Instanciar o DistanceMatrixService
var service = new google.maps.DistanceMatrixService();
//executar o DistanceMatrixService
service.getDistanceMatrix(
{
//Origem
origins: [$("#txtOrigem").val()],
//Destino
destinations: [$("#txtDestino").val()],
//Modo (DRIVING | WALKING | BICYCLING)
travelMode: google.maps.TravelMode.DRIVING,
//Sistema de medida (METRIC | IMPERIAL)
unitSystem: google.maps.UnitSystem.METRIC
//Vai chamar o callback
}, callback);
}
//Tratar o retorno do DistanceMatrixService
function callback(response, status) {
//Verificar o Status
if (status != google.maps.DistanceMatrixStatus.OK)
//Se o status não for "OK"
$('#litResultado').html(status);
else {
//Se o status for OK
//Endereço de origem = response.originAddresses
//Endereço de destino = response.destinationAddresses
//Distância = response.rows[0].elements[0].distance.text
//Duração = response.rows[0].elements[0].duration.text
var km = response.rows[0].elements[0].distance.text;
var exibe = km.replace(/\D+/g, '');
var valor_km = 5;
var calc = exibe*valor_km;
$('#litResultado').html("<strong>Origem</strong>: " + response.originAddresses +
"<br /><strong>Destino:</strong> " + response.destinationAddresses +
"<br /><strong>Distância</strong>: " +response.rows[0].elements[0].distance.text+
"<br /><strong>Distancia²</strong>: " +exibe+
"<br /><strong>Valor</strong>: "+"R$"+calc+",00"
);
//Atualizar o mapa
$("#map").attr("src", "https://maps.google.com/maps?saddr=" + response.originAddresses + "&daddr=" + response.destinationAddresses + "&output=embed");
}
}
</script>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<td>
<label for="txtOrigem"><strong>Endereço de origem</strong></label>
<input type="text" id="txtOrigem" class="field" style="width: 400px" value="12020-010" />
</td>
</tr>
<tr>
<td>
<label for="txtDestino"><strong>Endereço de destino</strong></label>
<input type="text" style="width: 400px" class="field" id="txtDestino" value="12030-470" />
</td>
</tr>
<tr>
<td>
<input type="button" value="Calcular distância" onclick="CalculaDistancia()" class="btnNew" />
</td>
</tr>
</tbody>
</table>
<div><span id="litResultado"> </span></div>
<div style="padding: 10px 0 0; clear: both">
<iframe width="750" scrolling="no" height="350" frameborder="0" id="map" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?saddr=são paulo&daddr=rio de janeiro&output=embed"></iframe>
</div>
</body>
</html>Discussão (1)
Carregando comentários...