Find places close to a certain coordinate

Asked

Viewed 173 times

0

I would like to find places close to a certain geographical co-ordination.

I have a database with a list of reference points and their respective coordinates.

Ex.:

var points = [
  ['cristo redentor', '111111', '222222']
  ['parque do ibirapuera', '111111', '222222']
  ['museu do ipiranga', '111111', '222222']
  ['av paulista', '111111', '222222']
]

When the user searches for a reference point, I would like to find all other points that are close to that chosen coordinate.

  • @Inhares the context is the same but they are distinct languages! :(

  • blz. I’m trying to find something, but so far nothing very useful

  • Thank you so much @rLinhares :)

1 answer

1


Follow a function to calculate the distance in KM, if you need the distance in Miles, just use the other variable already declared in the last multiplication of the method output.

Follow example of code call as well. Latitude 1: -23.5415, Longitude 1: -46.5786, Latitude 2: -22.1410, Longitude 2: -46.0216

function btnCalcular() {
        var result = CalcularDistancia(
            $("#latitude1").val(),
            $("#longitude1").val(),
            $("#latitude2").val(),
            $("#longitude2").val()
            );

        alert(result);
};
        
function CalcularDistancia(
Latitude1 ,
Longitude1 ,
Latitude2 ,
Longitude2
)
{
<!-- CONSTANTES -->
var RaioTerraEmML = 3963.1
var RaioTerraEmKM = 6377.99121
var PI  = Math.PI;

<!-- RAIO  -->
var lat1Radians ;
var long1Radians ;
var lat2Radians ;
var long2Radians ;

 lat1Radians = Latitude1 * PI / 180;
 long1Radians = Longitude1 * PI / 180;
 lat2Radians = Latitude2 * PI / 180;
 long2Radians = Longitude2 * PI / 180;

return Math.acos(
Math.cos(lat1Radians) * Math.cos(long1Radians) * Math.cos(lat2Radians) * Math.cos(long2Radians) + 
Math.cos(lat1Radians) * Math.sin(long1Radians) * Math.cos(lat2Radians) * Math.sin(long2Radians) + 
Math.sin(lat1Radians) * Math.sin(lat2Radians)
) * RaioTerraEmKM;

}
        
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<label>latitude1 </label><input type="text" id="latitude1" value="" />
    <label>longitude1 </label><input type="text" id="longitude1" value="" />
    <label>latitude2 </label><input type="text" id="latitude2" value="" />
    <label>longitude2 </label><input type="text" id="longitude2" value="" />

    <input type="button" name="btnCalcular" onclick="btnCalcular()" value="Calcular" />

  • Thanks @Alexandre very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.