Location retrieval
This feature allows you to get the geographic coordinates of a device and exhibit it on the screen. You will also need to specify a certain amount of time or maximum time limit for the location information you receive. This way, your device's location will always be up to date!
NOTE: Querying a device's location might have serious privacy implications. So, it should be done carefully and strict authorization may be needed.
Retrieving and displaying a device's location
Retrieving the location of a device can be fairly simple.
Assuming a device was already created in this previous step,
a location
object, as the one below,
will be instantiated from the fields for longitude and latitude.
You can use the following code snippet to exhibit a device's geographic coordinates and address on the screen:
import network_as_code as nac
from network_as_code.models.location import CivicAddress, Location
from network_as_code.models.device import Device, DeviceIpv4Addr
# Give the device identifier and SDK token
client = nac.NetworkAsCodeClient(
token="<your-application-key-here>",
)
device = client.devices.get("device@testcsp.net",
ipv4_address = DeviceIpv4Addr(public_address="233.252.0.2",
private_address="192.0.2.25",
public_port=80)
)
# max_age is a mandatory parameter
# The location object contains fields for longitude and latitude
location = device.location(max_age=60)
longitude = location.longitude
latitude = location.latitude
print(longitude)
print(latitude)
print(location.civic_address)
The output shown on the screen with the values for latitude, longitude and civic address should be like this one:
00.00000000000000
00.00000000000000
civic_address=CivicAddress(country='US', a1='City name.', a2='Street name 00-00.', a3=None, a4=None,
a5=None, a6=None)
Device object parameters
The snippet above identified a mobile network device in multiple ways (IP addresses, port, etc). Learn how to create a device object and understand how the DeviceIPv4Addr
model works using NAT technology.
Location retrieval parameters
The device.location()
method, mentioned in the code above,
will require the following mandatory parameter:
Parameters | Description |
---|---|
max_age=60 | Maximum Age: the maximum age accepted for the location information request. Here you should inform the integer value expected in seconds. Let's suppose you don't want to receive retrieve a location which is older than 1 minute. Then, defining the value in seconds, 60 , will check if the device was at the location the informed location at least 1 minute ago. |
Good to know: If the location information is older than the one accepted, which was defined with the
max_age=60
parameter, then the output of request will beUNKNOWN
.
Remember: The location data does not update automatically. So, in order to get an up-to-date device location, just fetch it again:
location = device.location()
print(location)
Important to keep in mind: The location data might be more accurate in areas with a dense concentration of base stations, but less accurate in more sparse or remote areas.