1

I want to get all the lat and long of the user with in 1 km distance. For example i have the user current location latitude and longitude i want to find all of his friends with in 1 km distance.

User1 current Latitude : '25.276987'
User1 current Longitude: '55.296249'

Similarly I have all the other users lat and long with me.

{'user2':{lat:'25.122212','long':'55.296249'},'user3':{lat:'25.222212','long':'55.396249'}}

I want to find all the other users who are in 1 km distance of user1.

i tried this but did not help me.

Any help would be greatly appreciated. Thanks..

Sakeer
  • 1,885
  • 3
  • 24
  • 43

1 Answers1

2

I think, it should be other way round. You can calculate the distance between the User and his/her friends, and determine if they are in 1 KM of the user. You can simply do it like this(I am using GeoPy for this example):

from geopy import distance

def get_distance(user_co, friend_co):
    coords_1 = (user_co.get('lat'), user_co.get('long'))
    coords_2 = (float(friend_co.get('lat')), float(friend_co.get('long')))

    return distance.distance(coords_1, coords_2).km


user = {'lat': 25.276987, 'long': 55.296249}
friends= {'user2':{ 'lat':'25.122212','long':'55.296249'},'user3':{ 'lat':'25.222212','long':'55.396249'}}

for key, value in friends.items():
    dstnce =  get_distance(user, value)
    if dstnce < 1:
        print("Friend {} is in 1 KM".format(key))

You can look into this so answer for more details on distance calculation.

ruddra
  • 50,746
  • 7
  • 78
  • 101