문제

I'm building a mobile app, and one area I'm not quite sure how to tackle is the facility to find the closest location in the locations object to the user's current location.

For example, when a user taps 'Find nearest', their long and lat is stored as variables. I have an object that has data for locations the app can use in this structure:

locations
-123
--Name: Llandudno
--Long: 12345
--Lat: -23455
-124
--Name: Conwy
--Long: 12347
--Lat: -23459

How can I (using jQuery) find which location is closest to the user, or can this not be done using some form of mathematical equation? If so, what does anyone recommend?

Thanks in advance for your help

도움이 되었습니까?

해결책

I believe the answer you're looking for extends beyond just jQuery. While JavaScript/jQuery will certainly be involved for interacting with the Google map object, the client-side languages may not be powerful enough to complete this task.

The most complete solution I've found to this question is actually provided by Google:

That page provides detailed instructions on how to create a "Store locator" (or whatever you're locating) using PHP, MySQL, and Google Maps.

That page also links to a good article detailing the algorithms involved:

The calculations are fairly in-depth, but the page does a great job of explaining the logic and even provides JavaScript in case you'd like to create your own solution.

Good luck!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top