2

hi i am using this map

https://google-developers.appspot.com/maps/articles/mvcfun/step6

i can get the latitude and longitude of the required location also i can get the distance (radius). so i have a database in which my each record have latitude and longitude. i want to search the record within the range selected on the map. how the sql query will work ?

for better explanation my each record have a latitude and longitude. user will select the map and i want to search the record within selected range.

Ahmad Gulzar
  • 355
  • 2
  • 8
  • 23
  • get the distance from your point to each record if it is in your radius means less than your radius show it. – Satish Sharma Feb 17 '14 at 09:14
  • You know what you want to do. What is the query you wrote? – Kuzgun Feb 17 '14 at 09:14
  • @SatishSharma - And what's the calculation for distance on a geode? And how do you avoid a full table scan for every single query? – MatBailie Feb 17 '14 at 09:19
  • @MatBailie distance can be calculate by simple math of distance between two points. and yes it will slow the process because of fetching each record and calculate the distance with it. – Satish Sharma Feb 17 '14 at 09:22
  • @MatBailie bro i want to discuss with you that if we use a procedure for it. can we do with it? will it increase the performance? – Satish Sharma Feb 17 '14 at 09:23
  • @SatishSharma - Not on a geode it can't. It's is certainly not `(a^2 + b^2)^0.5`on a curved surface. – MatBailie Feb 17 '14 at 09:23
  • @MatBailie i had already did it with lat and lng in decimal points – Satish Sharma Feb 17 '14 at 09:25
  • i am not saving the distance in database. now i am just saving the latitude and longitude value. and there will be thousands of records – Ahmad Gulzar Feb 17 '14 at 09:34
  • i think circle is not good for search. it will be better for me to use rectangle for this purpose. i got some idea from this post http://stackoverflow.com/questions/17773694/returning-results-within-latitude-longitude-rectagle?rq=1 – Ahmad Gulzar Feb 17 '14 at 09:37

1 Answers1

2

The way I have always done this is to use the maps api to draw a circle with the required radius, find the lat long bounds of this circle and then query the database:

SELECT * FROM yourtable WHERE lat BETWEEN a AND c AND lng between b AND  d

Where a and b are you top left coordinates and c and d are your bottom right. (more information available here).

Now that you have all objects within your bounding box you will need to pass them back to your front-end and determine whether or not they are within your radius. See this answer for more information.

Community
  • 1
  • 1
ChrisSwires
  • 2,713
  • 1
  • 15
  • 28