Archived:How to get the lat-long using an external GPS in PySymbian
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.
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()) #selecting
conn = socket.socket(socket.AF_BT, socket.SOCK_STREAM)
to_gps = conn.makefile("r", 0)
msg = to_gps.readline()
if msg.startswith("$GPGGA"): #search for $GGPGA
gps_data = msg.split(",") #detects the comma
lat = gps_data #get the latitude
lon = gps_data #get the longitude
print "You are now at latitude %s and longitude %s" % (lat, lon)
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:
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.