2

After multiple attempts to figure out how to find the closest gps point from the lat & lng fields (field_data_field_lat & field_data_field_lng) I would like to ask if such is possible? Is there any module that provides a solution with which it's able to create two fields for lat/lng and use them at views to find the closest one from an exposed filter?

I assume that there is no such ready made solution. In that case, I would like to write my custom sql statement for D7. Unfortunately, D7 doesn't support the D6 sql query functions and also the pager_query function.

Therefor I would like to write a statement that would bind the lat and lng fields and give back the nearest - closest gps points. Similar, one field statement is written at: http://www.techfounder.net/2009/02/02/selecting-closest-values-in-mysql/ where the union and ABS is used. I've found the union option but didn't figured out how to make the comparison for both fields within the D7 db sql layer.

Thank you in advance.

3 Answers 3

2

Have a look at this article: Creating a Store Locator with PHP, MySQL & Google Maps. Especially the section about "Finding locations nearby with MySQL" is hinting towards your solution.

As you can read in the mentioned article, calculating the distance between 2 points given their lat/lon requires quite some math (Haversine formula). So, I guess that the performance optimization in the article you are referring to won't be possible. This because it will be hard to use an index to retrieve probable points.

2

There's a module for that.

Location: "[..] At present, it is the only module that provides the views integration necessary for implementing location-based searches and is designed for the purpose of finding points on a map within a vicinity of a user-supplied location."

If you need to have separate form fields for lat and long, and Location doesn't provide that, you could write your own widget/formatter.

1
  • +1 for "There's a module for that"...Drupal should look into trademarking that phrase :)
    – Clive
    Commented Feb 4, 2012 at 20:03
2

I needed this myself, and implemented it for Geofield. I posted my patch here. You should be aware that I only started the functionality, and the patch happens to work on my site, but needs quite a bit of work to be suitable for inclusion into Geofield. Perhaps you can use it though. Bonus points for posting back an improved patch to the issue.

If you read the rest of the issue you'll also find a discussion on other ways of solving your problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.