Question

I'd like to calculate the closest country (as viewed on a world map) in a given direction (provided in degrees) from a user's current location.

I realize one way of doing this is to use the formula provided here to step in, for example, 5-mile increments from point to point until I finally reach a country that is not the user's starting country. However, that seems horribly inefficient with regard to use of geocoding resources.

Do any of you know of a better algorithm I could use for this?

Thanks in advance.

Was it helpful?

Solution

One way to reduce the amount of reverse geocoding operations is to treat this problem as a search for the border. If you use a binary search algorithm, and reverse geocode each point, you find where the country changes from your current country to the adjacent country with a minimum number of reverse geocode operations.

In the binary search, your heading is constant, and you have a minimum range (5 miles) and a maximum range (12,000 miles), you are searching for the range at which the border lies. Then you reverse geocode a position just beyond the border to find out what country is there. One problem is that just beyond the border might be ocean.

OTHER TIPS

I would use MKReverseGeocoding. Check this SO question for code examples.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top