Challenge: "Closest" location lookup using Longitude / Latitude

bnels

New Member
Joined
Aug 21, 2014
Messages
19
I have a list of a number of locations and their corresponding Longitude and Latitude.

I need to be able to enter current Longitude and Latitude from a GPS unit into a form and give back the closest location from the Excel table (nearest match by distance for both Long and Lat)

One of the tricks gets to be if for example the desired longitudes are similar for multiple locations but the latitudes are different.

However I have no idea where to start. I was thinking of trying to calculate actual distance from desired location to each of the locations in the list (not even really sure how to convert long/lat to feet) but that didn't seem as efficient.

Is there a way to do this with Index/Match or an array formula or something?

Does anyone have any ideas?
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi, there would be a cool (ahem) way of doing this but may I ask if you already have a list of possible locations with their respective latitude and longitude?
We could then use a formula to compute the distance and indeed an index system to return a single or multiples answers (in case of a tie)
 
Upvote 0
Ok noted, do you want only one (1) result of several result, example all possible closest locations within ±50km...
 
Upvote 0
Looking for single closest location...

I did find this formula to calculate distance between pair of long/lat coords in feet which got me close. Putting this formula in an adjacent column and filling down. How do I pick the closest coordinates in table (ie. lowest calculated formula value)

entry long lat: B2=lat1, c2=long1
table data b5=lat2, c5=long2

=(ACOS(COS(RADIANS(90-$B$2))*COS(RADIANS(90-B5))+SIN(RADIANS(90-$B$2))*SIN(RADIANS(90-B5))*COS(RADIANS($C$2-C5)))*3958.756)*5280
 
Upvote 0
given as follows:


Excel 2013
ABCDEFG
1Loc_IDlongitudelatitudedistance6371
203N11.222169.85211334.8
307FA25.325393-80.27477516286.79
407MT48.4211069-106.527703919029.28
50WI843.1388947-88.472320617191.8
618AZ33.8180947-111.897924219794.49
71AZ033.111944-112.26916619833.16
820GA33.7565556-84.885062516790.79
92XS829.229405-99.82394718446.13
103N05.63167168.12511038.09
115A859.2826-158.61814717.25
1266CA35.607748-121.11019219206.17
137FA124.6487544-81.579808316432.3
list


then:


Excel 2013
ABCD
1longitudelatitude
2search33.8166566.11817
3resultsOATN32.60527865.864167
result


adjust range to fit your model
 
Upvote 0
For whatever reason the formulae are not included...
In sheet 'List'
Name range RadiusEarth refers to cell G1
Formula in D2 is =RadiusEarth*((2*ASIN(SQRT((SIN((RADIANS([@latitude])-RADIANS(lat_2))/2)^2)+COS(RADIANS([@latitude]))*COS(RADIANS(lat_2))*(SIN((RADIANS([@longitude])-RADIANS(long_2))/2)^2)))))

In sheet 'result'
Name range long_2 is cell C2
Name range lat_2 is cell D2
Formula in B3 is =INDEX(Table1[Loc_ID],MATCH(SMALL(Table1[distance],ROWS(result!B$3:B3)),Table1[distance],0))
Formula in C3 is =INDEX(Table1[longitude],MATCH(result!B3,Table1[Loc_ID],0))
Formula in D3 is =INDEX(Table1[latitude],MATCH(result!B3,Table1[Loc_ID],0))
 
Upvote 0
Couldn't quite get your formulas to work in my table but you gave me the INDEX formula I needed to add to my distance formula to pick the closest from the list so I got it to work - much appreciated!

My solution using your INDEX formula from B3 above...

INPUT LONG/LAT (FIND ID & ACCURACY):
CLOSEST_ID Long1 Lat1 Accuracy (ft)
9+02 -88.### 40.### 2.5 <<< Used "MIN" formula to show distance to closest ID

CLOSEST_ID=INDEX(Table1[ID],MATCH(SMALL(Table1[distance],ROWS(A$2:A2)),Table1[distance],0))


CALC TABLE:
ID long2 lat2 distance
7+56 -88.##### 40.##### 193.26352777

DISTANCE=(ACOS(COS(RADIANS(90-$B$2))*COS(RADIANS(90-B5))+SIN(RADIANS(90-$B$2))*SIN(RADIANS(90-B5))*COS(RADIANS($C$2-C5)))*3958.756)*5280
 
Upvote 0
Couldn't quite get your formulas to work in my table but you gave me the INDEX formula I needed to add to my distance formula to pick the closest from the list so I got it to work - much appreciated!

My solution using your INDEX formula from B3 above...

INPUT LONG/LAT (FIND ID & ACCURACY):
CLOSEST_ID Long1 Lat1 Accuracy (ft)
9+02 -88.### 40.### 2.5 <<< Used "MIN" formula to show distance to closest ID

CLOSEST_ID=INDEX(Table1[ID],MATCH(SMALL(Table1[distance],ROWS(A$2:A2)),Table1[distance],0))


CALC TABLE:
ID long2 lat2 distance
7+56 -88.##### 40.##### 193.26352777

DISTANCE=(ACOS(COS(RADIANS(90-$B$2))*COS(RADIANS(90-B5))+SIN(RADIANS(90-$B$2))*SIN(RADIANS(90-B5))*COS(RADIANS($C$2-C5)))*3958.756)*5280
Glad you got it sorted out.
Formula given would give distance in km
Min would work as well
Solution in B3 would have given second closest distance if dragged down (and do on).
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,945
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top