Add new search and sort by geolocation feature #2096
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description of the Change
Adds a new feature for sorting and restricting search results by geolocation. This functionality creates a "locate Me" button that a user can click to set their latitude and longitude coordinates. The location of the locate me button as well as the text can be customized via the Elasticpress dashboard and various filters. The coordinates are passed via query parameters, and a cookie to allow ElasticSearch to find results near the user's location and sort by distance. Elasticsearch will index and query custom meta fields called "latitude" and "longitude" (that are not currently created by this code but could be) and calculate what posts are most relevant to the user's location. By default this will limit results to 30 miles of the user's location, but that can be changed with a filter.
Alternate Designs
This does not currently allow for searching by zip because reliable services that convert lat long to zip cost money. We can add a field for users to add a google maps api key and we could use google maps to create search by zip functionality (https://developers.google.com/maps/documentation/geocoding/overview#ReverseGeocoding). A project I have worked on used a json list that we had locally, but we had to purchase that information.
Benefits
This is a very useful feature, for any organization that has physical locations that users might be looking for.
Possible Drawbacks
This does require some developer setup at the moment, mainly for custom meta fields, but that can be reduced.
Verification Process
I have tested this code locally with my own location and multiple posts that contain latitude and longitude meta fields. I set up a few posts that contained latitude and longitude information at varying distances from my own lat long. Some were within 30 miles of me and some were further. I got the location coordinates from google maps for testing. (If you click on a location, you can grab the lat long in the url.)
In the elasticpress dashboard, I enabled the gelocation feature, and added a class of a div within my search form, where I want the locate me button to appear. Then I re-indexed my posts. Next, I went to the frontend (which has to be https) and clicked the locate me button. It found my location and updated it's text to let me know that it was using my location. I entered the search term "test" in the field to search for my tests posts within 30 miles of me. When results were fetched, they were sorted by closest to furthest and they excluded results that were more than 30 miles away. When I checked debug bar with the elasticpress extension, I saw that the "sort" information for each post contained the calculated distance based on my latitude and longitude.
Checklist:
Applicable Issues
Changelog Entry
Added search by geolocation feature