Archived:How to get the lat-long using an external GPS in PySymbian
All PySymbian articles have been archived. PySymbian is no longer maintained by Nokia and is not guaranteed to work on more recent Symbian devices. It is not possible to submit apps to Nokia Store.
Article Metadata
Contents |
Introduction
Location based services are one of the most attractive and effective field. When talking about location based services(LBS) the first two parameters on which our application depend are latitude and longitude. In location based services the main aim is to get the location of the user and then use it for different applications. This code snippet will help in achieving this basic task of getting lat/long using a external GPS(Global Positioning Device) device. The GPS will be connected to your Mobile through Bluetooth.
Code Snippet
import socket
address, services = socket.bt_discover() # search for the GPS device
print "Discovered: %s, %s" % (address, services) #will print the name of the device
target = (address, services.values()[0]) #selecting
conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
conn.connect(target)
to_gps = conn.makefile("r", 0)
while True:
msg = to_gps.readline()
if msg.startswith("$GPGGA"): #search for $GGPGA
gps_data = msg.split(",") #detects the comma
lat = gps_data[2] #get the latitude
lon = gps_data[4] #get the longitude
break
to_gps.close()
conn.close()
print "You are now at latitude %s and longitude %s" % (lat, lon)
Explanation
The code above will get you the exact position in form of latitude and longitude. But the important thing is that through the external GPS device we will get the data which follos a standard defined by the National Marine Electronics Association(NMEA). The data are called NMEA sentences and are something like shown below:
$GGPGA,321123,1234,567,N,1131,101,E,2,88,0.9,555.45,M,47.9M,*47
The GPS sends something like shown above and it send one message per line. The single line contains many fields separated by a comma(,). The above line is GGPGA line and contains 15 fields the one from 3rd to 6th is of our important as it contains latitude and longitude information so using the code we read only that and get the location information. Except the GGPGA line there are many other lines which are described in the NMEA sentences and they provide lots and lots of good information.


17 Sep
2009