Welcome Guest, Not a member yet? Register   Sign In
Google Map Database Order By Distance?
#1

[eluser]ShoeLace1291[/eluser]
I'm developing a site that uses the CI google map api library. I was wondering if there was a way to pull the database entries and display them by distance from the user's address?
#2

[eluser]Derdiusz[/eluser]
First - convert user address to LON and LAT pair.
Next, use some Pythagoras Wink

You probably have a table with geocodes:

Code:
CREATE TABLE GEOCODES (
          address varchar(255) NOT NULL default '',
          lon float default NULL,
          lat float default NULL,
          PRIMARY KEY  (address)
        );

To get geocodes from table based on distance you need make something like this:

Code:
$uLon = ...; // user longitude
$uLan = ...; // user latitude

$this->db->query("select * from geocodes order by (SQRT(POW(($uLon-lon),2)+POW(($uLat-lat),2)))")->result_array();
It based on Pythagoras: x2+y2=z2.
Code:
POW(($uLon-lon)
is a power of longitude distance (x2)

Code:
POW(($uLat-lat)
is a power of latitide distance (y2)

So, z2 (distance) is
Code:
SQRT(POW(($uLon-lon),2)+POW($uLat-lat,2))

I hope it helps and works Smile
#3

[eluser]CroNiX[/eluser]
Thank you for that. That will come in very useful for something I will be working on soon.




Theme © iAndrew 2016 - Forum software by © MyBB