Question

What I have here is a program that pulls some info from a json object and displays it on a page. In this case Public Bike Stations. The json object provides a list of each stations latitude and longitude, so finding the points is not an issue.

What I am doing is trying to filter the list to only show stations that are within a rectangle of a user inputted lat and long coords.

I have no idea how I should filter out the lat and long from the json list that exceeds my rectangle

      $(document).ready(function() {
        $("#createlist").click(function() {
            var upperleftlatitude = Number($("#upperleftlatitude").val());
            var upperleftlongitude = Number($("#upperleftlongitude").val());    
            var lowerrightlatitude = Number($("#lowerrightlatitude").val());    
            var lowerrightongitude = Number($("#lowerrightongitude").val());    

And this is where I am confused. I figured I would just write an if statement that said if the json lists lat and long was within the area of the rectangle based on the coords given by the user to display only these stations but I have no idea how to write it

                  if ( ){
                    var table_row = "<tr class='stationrow'>"
                    table_row += "<td>" + station.stationName + "</td>";
                    table_row += "<td class='numeric'>" + station.availableDocks + "</td>";
                    table_row += "<td class='numeric'>" + station.availableBikes + "</td>";
                    table_row += "<td class='numeric'>" + station.latitude + "</td>";
                    table_row += "<td class='numeric'>" + station.longitude + "</td>";
                    table_row += "</tr>";
                    $("#stationtable").append(table_row);
                }   
            total_bikes_available += station.availableBikes;
            });
        $("#totalbikesavailable").html(total_bikes_available);
        });
    });
});
Était-ce utile?

La solution

Assuming that the coordinates you want to check are station.latitude and station.longitude, and the four sides are named as you indicated in your code, then the condition becomes

if(station.latitude < lowerrightlatitude && station.latitude > upperleftlatitude && station.longitude < upperleftlongitude && station.longitude > lowerrightlongitude)

This is assuming that the names of your variables are sensible indications of where they are in the rectangle. You might want to convince yourself that this still works when the sign changes (use -ve values for West longitude, and South latitude, and you should be OK).

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top