Reputation: 759
I am trying to make an endpoints where by I can get the longitude and latitude of a User(Rider) in Real Time and save it to my Database. I have tried Researching about how to go about this, but I have only been able to get How to receive the current location which isn't in real-time as I want. I would be glad if anyone could directs me on how to go about this and provide some materials where i could read more about how to achieve this or a past work on this.
Thanks in anticipation.
Upvotes: 1
Views: 2104
Reputation:
Not quite sure what you mean, getting live location is a front end matter that then sends it to the BackEnd so you need to check out how to enable user location access in the library you're using in your front (react,react native,vue...) if you then later need to convert address or lat/long back and fourth I recommned starting with the Google Maps API, you'll need to generate an API Key in that dasboard (free use to certain limit)
then you can use it to get certain data about a location, for example, get it's elevation:
RESULTS = 'results'
LOCATIONS = '?locations='
KEY = '&key='
INDEX: int = 0
LOCATIONS = 'Locations'
def get_elevation(lat: float, lon: float):
"""
This function receives a Point and sends a request to Google's Elevation API.
@param float lat: latitude
@param float lon: longitude
@return float: elevation of point in meters
"""
url = "https://maps.googleapis.com/maps/api/elevation/json"
request = urlopen(url + LOCATIONS + str(lat) + "," + str(lon) + KEY + _GOOGLE_API_KEY)
results = json.load(request).get(RESULTS)
elevation = 0.0
if INDEX < len(results):
elevation = results[INDEX].get(ELEVATION)
print(elevation)
return elevation
It can also be used to retrieve an address from lat long and vice versa and so on..
you can also keep a model like so in your app:
class GeoLocation(models.Model):
latitude = models.DecimalField(max_digits=13, decimal_places=10, null=True, blank=True)
longitude = models.DecimalField(max_digits=13, decimal_places=10, null=True, blank=True)
street = models.CharField(max_length=400, blank=True)
city = models.CharField(max_length=255, blank=True)
state = models.CharField(max_length=255, null=True, blank=True)
country = models.CharField(max_length=10, choices=country_names.items(), blank=True, null=True)
def __str__(self):
return f'{self.street}, {self.city}, {self.country}'
def save(self, *args, **kwargs):
# this is just an example use
if all([self.street, self.city, self.country]) is None:
# haven't implemented a function like this myself but it just a way to use thie model
self.street, self.city, self.country = get_address_from_lat_long(self.latitude, self.longitude)
elif all([self.latitude, self.longitude]) is None:
# using the str(self) will return the full address, which google maps has endpoint for
self.latitude, self.longitude = get_lat_long_from_address(str(self))
Upvotes: 1