我希望我的用戶查詢兩個slugs字段(緯度,經度),然後將2個slug字段進行比較,以找到1.5km半徑範圍內的最近距離,並根據最近的安全屋顯示api。 例如:當用戶添加經度,緯度在查詢中,查詢REST API經緯度
www.example.com/safeplace/?find=-37.8770,145.0442
這會顯示內1.5公里
這裏最近的safeplaces是我的功能
def distance(lat1, long1, lat2, long2):
R = 6371 # Earth Radius in Km
dLat = math.radians(lat2 - lat1) # Convert Degrees 2 Radians
dLong = math.radians(long2 - long1)
lat1 = math.radians(lat1)
lat2 = math.radians(lat2)
a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLong/2) *
math.sin(dLong/2) * math.cos(lat1) * math.cos(lat2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
d = R * c
return d
這裏是我的模型
class Safeplace(models.Model):
establishment = models.CharField(max_length=250)
address = models.CharField(max_length=250)
suburb = models.CharField(max_length=250)
postcode = models.IntegerField()
state = models.CharField(max_length=250)
type = models.CharField(max_length=250)
latitude = models.DecimalField(decimal_places=6,max_digits=10)
longtitude = models.DecimalField(decimal_places=6,max_digits=10)
有沒有辦法在我的數據庫中運行for循環?我目前正在研究Django SQLite。在Views.py上,如何在用戶輸入中實現距離函數,在我的rest api url中查找最近的safeplace並顯示爲REST Api?
yourapi表示'WWW .yourapi.com/api/safeplace'? – Tushant