magento2 – Sort the product collection by Latitude and Longitude

I need to sort the product collection by latitude and longitude in the product list and the search page.


I have two product attributes Latitude and Longitude. All products have a unique latitude and longitude. Now, on the product list page, we need to sort the product collection by current latitude and longitude.

Suppose my latitude of current location is 36.778259 and the longitude is -119.417931, In the product list page All products with latitude 36.778259 and longitude -119.417931 must first appear in the list page and the other products close by latitude and longitude must be displayed respectively and so on. I guess we have to use Google Map API to do it, but we do not know how to do it.

Please provide me with solutions. Any help would be appreciated! Thank you!