![]() |
![]() |
|
Deadline To give more time to developers |
GPS
Welcome to another Cell•SDK tutorial.
Once upon a time Willy Fog went around the word in eighty days, but if he had got Cell•SDK, well, that would be another story. So let Mr. Fog to teach us in the use of GPS in Cell•SDK.
Mr. Fog:
Hello dears! The world I have always been a wordless man, so just create a new Cell•SDK project to see the GPS functionality in action.
We are going to access the device’s GPS to get the latitude and longitude to show it in the screen, so add two labels as private class variables to be able to represent the data in some Update’s method execution. I have said “some” because we shouldn’t do it in every execution, we will refresh the data every second, so we will need a timer too. So add these class variables:
private Label lblLatitude, lblLongitude; private TimeSpan timer;
Now create, and add the Labels to the screen with this lines in the Initialize method:
lblLatitude = new Label("Latitude: ");
lblLongitude = new Label("Longitude: ");
AddComponent(lblLatitude, Preferences.Width / 4, Preferences.Height / 8);
AddComponent(lblLongitude, Preferences.Width / 4, Preferences.Height / 4);
We have used Preferences.Width/X structure due to different device screens resolutions.
Ok, now remember, as you saw in other tutorials, that accessed the device hardware we will need to initialize the device and stop it at the application exit at least.
We need to add a reference to the LocationSystem dll which allow us to use the GPS that can be located under Cell•SDK/Versions/ and add a using statement like:
using Syderis.CellSDK.IO.LocationSystem;
To initialize the GPS and start to be able to read data we call, but do not forgot to check if GPS is connected. Add this class variable:
private bool isConnected;
Check if GPS is connected:
if (isConnected)
LocationSensor.Instance.Start(LocationSensors.GPS);
else
Preferences.App.Alert("GPS sensor is not available on this device", "Warning!");
With that line, the GPS start to read the information needed to be capture.
So it is time to represent it. We will override Update() method to set the label text only every second using the timer we created before:
public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
{
base.Update(gameTime);
if (!isConnected)
return;
timer += gameTime.ElapsedGameTime;
if (timer > TimeSpan.FromSeconds(1))
{
lblLatitude.Text = string.Format("Latitude: {0}", Syderis.CellSDK.IO.LocationSystem.LocationSensor.Instance.GeoLocation.latitude);
lblLongitude.Text = string.Format("Longitude: {0}", Syderis.CellSDK.IO.LocationSystem.LocationSensor.Instance.GeoLocation.longitude);
timer = TimeSpan.Zero;
}
}
If we run we will get this:

Once again, do not foget to stop GPS when you exit the application:
public override void BackButtonPressed()
{
base.BackButtonPressed();
if(isConnected)
LocationSensor.Instance.Stop(LocationSensors.GPS);
}
We can acces more information like altitude, course and speed. So it is your turn in making great applications that uses them.
"Keep on cool coding."
You can get the code in Github















