Find places near a given coordinate [duplicate]

0

I would like to find places close to a certain geographical coordinate.

I have a database with a list of landmarks and their 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.

    
asked by anonymous 08.09.2017 / 16:40

1 answer

1

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

Follow example 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><inputtype="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" />
    
08.09.2017 / 19:33