Search code examples
phpmysqlxmlsearchbitmapdata

Search approach for location based search


I need to implement a locality based search feature where users would be able to choose a city and locality(within the city) , proceed to mention a keyword and click on search . So say we have 10 cities with an average of 100 localities in each . The results are to be displayed considering the user provided locality as the starting point and ordering other results further away from the user's locality (in this way the cities results for the particular keyword would be constant only the ordering would change ) .What would be the best approach in php ?

1) Calculate the distance between various localities , pre store them in the mySqldatabase . This would mean a 100X100 matrix for the localities (each localities distance from the others).Also as many tables as the cities in the db . So 10 such tables + possibility of adding more . This is to be used only for ordering.

2) Put all the data in an XML instead of mysql database.

3) Use bitmaps and compressed images ?

Considering performance and scalability as a main criteria .


Solution

  • You're first filtering your data by city and tag, and then want to order the result by distance, but not filter by distance. I.e. even the farthest points should be retrieved from the database. In that case, there shoulnd't be too much of a penalty if you compute the distance from the given location when you execute the request. After all, it's just one formula evaluation per line, and then sorting the result by that value.

    If your data is given using latitude and longitude coordinates, then theoretically this would mean computating lengths of geodesics on the sphere using a complicated formula. But within a single city, the georgraphical grid can reasonably be assumed to be rectangular (not square!). So if you store the ratio between one degree of latitude and one degree of longitude for each city, then you can convert all coordinates to a square grid, compute difference from the entered location in x and y direction, square and add those. No need to take the square root, as ordering by squares works just as well.

    Something along these lines:

      SELECT location.name, location.lat, location.lon, …
        FROM locations, cities
       WHERE location.city = city.id
         AND city.name = ?
         AND location.tag = ?
    ORDER BY POW((location.lat - ?), 2) + POW((location.lon - ?)*city.geoaspect, 2)
    

    where parameters are the city name, the selected tag and the entered latitude and longitude.