I have this code
Calculate the distance between 2 points
async function CalcularAuto(restaurantes){
await geocode();
//await geodestination();
latB.push(restaurantes.Latitud);
lonB.push(restaurantes.Longitud);
var latLocation = latP;
var lngLocation = longP;
var latDestination = latB;
var lngDestination = lonB;
var getDistance = function(latLocation,lngLocation,latDestination,lngDestination) {
const deg2rad = deg => (deg * Math.PI) / 180.0;
var R = 6371;
var dLat = deg2rad(latDestination-latLocation);
var dLon = deg2rad(lngDestination-lngLocation);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) + Math.cos(deg2rad(latLocation)) * Math.cos(deg2rad(latDestination)) * Math.sin(dLon/2)
* Math.sin(dLon/2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d;
}
function de2rad(n){return n * (Math.PI/180)}
var d = getDistance(latLocation,lngLocation , latDestination,lngDestination );
latB = [];
lonB = [];
var distanciaTemp = 0;
console.log((d).toFixed(2)+ "km");
distanciaTemp = ((d).toFixed(2));
if(menor == 0){
menor = distanciaTemp;
latt = latDestination;
longg = lngDestination;
}
if(menor >= distanciaTemp){
menor = distanciaTemp;
latt = latDestination;
longg = lngDestination;
return restaurantes;
}
return null;
}
Takes the value that the input has and transforms it to coordinates
async function geocode(){
var location = document.getElementById('direccion').value;
//location = latP +","+ longP;
if (location == "") {
}else{
await axios.get('https://maps.googleapis.com/maps/api/geocode/json',{
params:{
address:location,
key:'[YOUR_API_KEY]'
}
})
.then(function(response){
// Log full response
// console.log(response);
// Formatted Address
var formattedAddress = response.data.results[0].formatted_address;
var formattedAddressOutput =
`
<ul class = "list-group">
<li class="list-group-item">${formattedAddress}</li>
</ul>
`;
// Address componets
var addressComponents = response.data.results[0].address_components;
var addressComponentOutput =
`
<ul class = "list-group">
`;
for (var i = 0; i < addressComponents.length; i++) {
addressComponentOutput +=
`
<li class="list-group-item"><strong>${addressComponents[i].types[0]}
</strong>: ${addressComponents[i].long_name}</li>
`;
}
addressComponentOutput += '</ul>';
// Geometry
var latLocation = response.data.results[0].geometry.location.lat;
var lngLocation = response.data.results[0].geometry.location.lng;
latP = latLocation;
longP = lngLocation;
/* var geometryOutput =
`
<ul class = "list-group">
<li id="latLocation" class="list-group-item"><strong>Latitude</strong>:
${latLocation}
</li>
<li id = "lngLocation" class="list-group-item"><strong>Longitude</strong>:
${lngLocation}
</li>
</ul>`;
*/
})
.catch(function(error){
// console.log(error);
})
}
}
take the data from an input and send it
This is the result from the console, effectively it is taking the smallest value, that is, the smallest distance and it shows it.
but there are times when this fails depending on the address, this is what it shows in the console
This is the code that asks if the minor is greater than the distance taken in distanceTemp, if it is, then the minor is assigned whatever the distanceTemp contains
if(menor == 0){
menor = distanciaTemp;
latt = latDestination;
longg = lngDestination;
}
if(menor >= distanciaTemp){
menor = distanciaTemp;
latt = latDestination;
longg = lngDestination;
return restaurantes;
}
return null;
for some strange reason, when asking if 6.81 is greater than 10.23, it says that it is and it shouldn't be.
another example:
if in the input I write as address "Soledad, Atlántico" this happens
These are the addresses that I have assigned in the database
This is the database query code and takes the addresses
async function consultarRestaurantes() {
fetch('http://localhost/ecommerce/ecommerce/ecommerce/api/consultar.php', {
method: "GET",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(respuesta => respuesta.json())
.then(restaurantes => {
agregarRestaurantesTabla(restaurantes);
})
}
Query.php
<?php
// http://localhost/sistema_php/api/consultar.php
$conexion = new mysqli("localhost", "root", "", "appecomerce") or die("not connected".mysqli_connect_error());
$sql = "SELECT * FROM `tbl_restaurantes`;";
$result = mysqli_query($conexion, $sql);
$restaurantes = array();
while ($fila = mysqli_fetch_array($result)) {
array_push($restaurantes, $fila);
}
echo json_encode($restaurantes);
mysqli_free_result($result);
mysqli_close($conexion);
?>
Format in Data Table Image Text
1 McDonals Calle 93 Cra. 46 #No. 93-10, Barranquilla, Atlántico Si 11.004814286810749 -74.82763763213357
2 McDonals Cl. 80 #No. 51B-10, Barranquilla, Atlántico 24 Horas 11.004039581617151 -74.81271143213358
3 Drogueria la rebaja plus Cra. 41 ##58-103, Barranquilla, Atlántico 24Horas 10.984260699978947 -74.79738559396759
4 FarmaTodo Carrera 65 # 86 - 125, Barranquilla Si 11.017597181486195 -74.81342602037543
5 Centro Comercial Nuestro Atlantico Av. Murillo #131 a 13-167, Soledad, Barranquilla, Atlántico 10.905670926327327 -74.80212241485586
6 Centro Comercial Portal De Soledad Av. Murillo #14, Barranquilla, Soledad, Atlántico 10.909393484447167 -74.79884602833592
According to your examples it is always bringing you the smallest... 0, 1... or whatever is smallest...
But if we do an analysis, notice:
the smallest is 1 followed by a 0
is 0..
Which gives me the impression, that you are taking the numbers as text.
So looking at your code, it shows that before comparing with the smallest, you do this:
And if we go to the definition of what it does, it says:
Therefore, since there is no static definition of the type of variables in js, and the function is converted to a string, then it is comparing as a string...
Recommendation, first do all the necessary comparisons everywhere, and lastly, do the transformation to show it...