My goal is to retrieve a user's lat/long position then find the nearest lat/long in my list of locations. Below is what I have and I believe it works however I am not sure if this is the best method to get to the end result I want.
I go through the list of locations converting them to absolute values then subtract out the user's location value to get the distance. If either the X oy Y is less than the previously recorded X or Y the distance variable gets updated with the new values.
This appears to work however, like I said, I am not sure if I went about it in the best manner possible. My list of locations will update every so often but it will not exceed 100 possible positions.
Thank you very much for your time.
locations = [(-71.43994800000002,41.6919549),
(-71.61075089999997,41.577545),
(-71.06653670000003,42.41383099999999),
(-71.41283429999999,41.8239891),
(-71.05888010000001,42.3600825),
(-74.00594130000002,40.7127837)]
userlocation = (-71.28254930000003,41.7303793)
distance = [999,999] #initial value holder for distance
for location in locations:
x = abs(location[0]) # absolute value of latitude
y = abs(location[1]) #absolute value of longitude
xu = abs(userlocation[0]) #absolute value of user's latitude
yu = abs(userlocation[1]) #absolute value of user's longitude
dx = x-xu #Subtract user from location X
dy = y-yu #subtract user from location Y
if dx < distance[0]: #if distance X is less than the current distance value
distance[0] = dx #update with new values
distance[1] = dy
continue #go to the next one
if dy < distance[1]: #if distance Y is less than the current distance value
distance[0] = dx #update with new values
distance[1] = dy
continue #go to the next one
print(distance) #print the end smallest result