嗨我試了很多,我不知道爲什麼當我想要保存haversine()
x給我以下錯誤:undefined Variable Haversine
。它工作的唯一辦法是,當我把半正矢函數內部得到本功能爲什麼我的方法不會調用另一個(未定義的變量)
class GetRide(APIView):
authentication_classes = (TokenAuthentication,)
permission_classes = (IsAuthenticated,)
def haversine(lat1, lng1, lat2, lng2):
"""
Calculate the great circle distance between two points
on the earth (specified in decimal degrees)
"""
# convert decimal degrees to radians
lng1, lat1, lng2, lat2 = map(radians, [lng1, lat1, lng2, lat2])
# haversine formula
dlng = lng2 - lng1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlng/2)**2
c = 2 * asin(sqrt(a))
km = 6367 * c
return km
def get(self, request, route_id):
d_route = Route.objects.get(id=route_id)
p_routes = Route.objects.all()
for route in p_routes:
x = haversine(d_route.origin_lat,d_route.origin_lng, route.origin_lat, route.origin_lng)
if (x < 3):
new_route = 0
return Response(new_route,status=status.HTTP_200_OK)
'self.haversine' –
它看起來像你的縮進是有點搞砸了。 haversine函數也應該把'self'作爲它的第一個參數,因爲它是一個類方法。你也應該用'self.haversine'呼叫haversine – fabianvf
謝謝,爲什麼我需要它從自我調用它。 ? –