Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new search and sort by geolocation feature #2096

Closed
wants to merge 5 commits into from

Conversation

PypWalters
Copy link

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:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my change.
  • All new and existing tests passed.

Applicable Issues

Changelog Entry

Added search by geolocation feature

@PypWalters PypWalters changed the title Feature/geolocation Add new search and sort by geolocation feature Feb 19, 2021
@mckdemps
Copy link
Contributor

mckdemps commented Mar 5, 2021

Adding this to EP labs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants