-3

I've got a db full of pretty geo locations. lng and lat's, the question is. I've found a nifty little sql select that will help me compare the geo locations, and pick the ones from my DB that are the closests, but im afraid i need to use my current geo location to find out which is nearest, so how would i go about putting it into the SQL selection?

Code below is the ' nifty ' select.

SELECT latitude, longitude, SQRT(
    POW(69.1 * (latitude - [startlat]), 2) +
    POW(69.1 * ([startlng] - longitude) * COS(latitude / 57.3), 2)) AS distance
FROM TableName HAVING distance < 25 ORDER BY distance;
  • 3
    `[startlat]` and `[startlng]` are just example/placeholder thingies. You should be able to take it from here... – Bojangles Nov 28 '12 at 13:22
  • startlat and startlng make up your current geo location. – Mike de Klerk Nov 28 '12 at 13:22
  • 1
    You will be glad to hear about [MySQL spatial extensions](http://dev.mysql.com/doc/refman/5.1/en/spatial-extensions.html). – moonwave99 Nov 28 '12 at 13:22
  • i know it needs to be replaced, but i am asking, how would i go about getting the result from JS and putting it in there? – user1856186 Nov 28 '12 at 13:23
  • 1
    @user1856186 That is a complete different question and has nothing to do with geolocation or PHP. Read some basics about HTML, JS, PHP and how they could work together. – feeela Nov 28 '12 at 13:26
  • It sort of is, i need to call the geo location out using JS and i can't make a php variable inside the JS to get the location into my select. – user1856186 Nov 28 '12 at 13:28
  • You mean how to use , for example , HTML5 Geolocation or something of that sort ? e.g how to send the Geolocation coordinates of your user to the server? – Joel Blum Nov 28 '12 at 13:28
  • …or you could use database that is able to handle geo-data (either as moonwave99 suggested or using [PostGIS](http://en.wikipedia.org/wiki/Postgis). – feeela Nov 28 '12 at 13:29
  • I assumed this was to be done in PHP? Why do so many people tag PHP when it's not PHP!!! Thanks for waisting my time! – HenchHacker Nov 28 '12 at 13:29
  • Also, warning to people going to answer - if it's not correct because of his poor explanation - he's quick to downvote!!! – HenchHacker Nov 28 '12 at 13:31

2 Answers2

0

Lots of ways you could do that i'd recommend fetching the geolocation of your current location first and storing lat/lng in a session/cookie or even in the database first. Then applying it to the SQL.

This might contain the answer - Get latitude and longitude automatically using php, API

Community
  • 1
  • 1
David
  • 34,836
  • 11
  • 47
  • 77
0

What you first need to learn is how to retrieve database information, through PHP, in your front end, by using AJAX for example. First know how to work with Database <-> PHP <-> Javascript. You can't do this: Database <-> javascript. So do not ask how to that as it is impossible.

Mike de Klerk
  • 11,906
  • 8
  • 54
  • 76