magento2 – Sort the product collection by latitude and longitude

I need to sort the collection of products by latitude and longitude in the list of products and on the search page.


I have two product attributes Latitude Y Length. Every product has unique latitude and longitude. Now on the product listing page we need to sort the collection of products by current latitude and longitude.

Suppose that my current location latitude is 36.778259 and the length is -119.417931, On the product listing page All products with latitude 36.778259 and length -119.417931 it should be displayed first on the list page and other products near latitude and longitude should be displayed respectively and so on. I guess we need to use the Google Map API to do it, but we're not sure how I do it.

Please, give me some solutions. Any help would be appreciated! Thank you!