In this lecture we are going to step back from augmented content for a moment to examine the extra functionality
that mobile devices provide that can be integrated into applications as input apart from touch controls.
Namely those of the building GPA and compass.
But before we use them let's take a look at what they are and how they work.
Starting with global positioning systems or GPA it's GPA.
It's a system of satellites surrounding the Earth.
Each satellite maintains a very stable atomic clock which they synchronize with each other and ground
clocks.
The satellites keep very accurate data about their position ground stations including the hardware in
my ball phones can query three or more satellites to get an accurate reading on their location.
On the surface of the earth the position coordinates on the earth are presented in a set of latitude
and longitude lines of latitude stretch around the earth like belt's the one brought in the very center
is the equator.
It represents zero degrees with the North Pole being 90 degrees north and the South Pole being 90 degrees
south.
Lines of long that you run vertically around the globe.
The zero degree line runs through Greenwich in England and is known as the prime meridian heading east.
The lines increase positively to 180 and heading west.
They count negatively to negative 180.
The distance between a single degree in a the latitude longitude is approximately one hundred and eleven
kilometers.
It's not precisely the same all over the globe because the Earth isn't a perfect sphere.
The Gold Coast where I live is situated at twenty eight point zero one six seven South.
One hundred and fifty three point four aist.
This is also expressed as negative twenty eight point 0 1 6 7 positive one hundred fifty three point
four as the latitude is below the equator and the longest route is east of Greenwich.
We're now going to build a really simple G.P.A. system and put it into your Android or iPhone device
using unity so Unity have all of the necessary commands to get the data that you want and then what
you can do is build it out to your device and go for a bit of a walk around and see how it updates the
coordinates.
So first of all you'll need a new unity project.
I'm working in a previous project I had that still has that we could use doesn't it.
You don't need.
We could choose for these.
OK so this is just a simple streite at Unity project with all of you're building tools for your mobile
device and you'll say I've already got mine switched over to Android and I'm doing this in portrait.
Everything's pretty much going to be done in code for this application.
The first thing we'll need in the hierarchy is to Right-Click and create an empty and let's call that
G.P.A. and that will hold onto our code that we're about to write.
So in the assets Right-Click create say Shut script and let's call it guess control.
They will drag and drop that onto and G.P.A. us object in the hierarchy case so it's now attached.
Right.
So let's open that up and start putting in some code.
The very first thing we'll put in here is a message string at the top of the code so it's a string message.
It's going to be global for a clos.
We're going to use it to write things into and then display them on your devices screen because you
don't have the luxury of the debug log to print things out to see them when you're wandering around.
So it's always good to get this stuff up on the screen so that you can at least debug it and say that
things are working for you.
So we're going to write anything we want to see on the screen into message.
Now as far as getting message to display on the screen we're going to use my very favorite old method
on gooey.
We're not going to use all the fancy graphical user interface stuff so just underneath that.
Let's put this in so it's voyde on.
And first of all I'm doing a gooey skin label dot font size and setting that to 50.
Now this size that you put here will depend on the resolution of your screen.
So I've got a new Samsung Galaxy and it's got a really high resolution beautiful screen actually.
Anyway I need a huge front to see what's written on it so the default font is way too small.
Just try this out for yourself a few times to get the right value.
Now the other thing I'm putting on here is a libel and this libel is just a rectangle that will hold
Al message.
The rectangle is going to be 30 in from the left and 30 down from the top of the screen is where it
will start and it's going to be 1000 by 1000 in size which could be a bit of an overkill if you don't
have it big enough for your width and height of this rectangle.
Then you will see your message get cut off.
So just be aware of these values also you can't I guess you can't really make them too big because they'll
just be off the screen so I guess your message from within won't wrap and go off the screen as well
so just keep an eye on those but make sure that they're big enough for your message.
We're now going to add on to heah a code routine that can run in the background because it needs to
ask the GPA system to initialize and then to get some values from it.
So just saying hey I'm going to insert these and then I'll come back and explain it all.
And so this really is the basics of the unity G.P.A. interface system.
Everything that you use is in import dock location and it's within say that you're going to find all
the commands for the GPS system.
So the first thing that we're doing is when this runs sitting and message from initializing it will
then say starting so that you know it's doing something then we go to an IF statement if not input that
location is enabled by user.
So you should know from having run other mobile apps that it will pop up on the screen and say Is it
OK if this app uses your location and you have to say yes or no.
Well in this case you will also have to say yes otherwise this is not going to work for you.
So these checks if it's enabled and if you try and run this in the energy editor you also get this message
because this G.P.A. code will not work on your desktop machine.
You will have to build this out.
Now if location services isn't enabled or you're on your desktop machine this whole thing just breaks
and gets out of this method right now moving down from there.
So assuming you've got location services enabled the next thing we're going to do is to stop them.
Now you have to start them in order for you to be able to query them and get values from them.
Now the Start method you can start with empty brackets and the two values and so I will default to 10
and 10.
The first value is how accurate you want to be.
Now if you don't really care about accuracy then you could put a really large number in here like 100
and that would be fun.
And I think it's something like 100 that it won't then rely so much on the power of your device and
it won't be constantly querying it to get a really accurate result.
So you can have location services that are going to give you a value that could be quite accurate but
it's can also be totally inaccurate up to like 100 meters.
This is five meters accuracy.
So saying you know I want it to be quite accurate.
The second is the displacement.
Now what this displacement does is it tells a location service how often it needs to update itself.
So if for example if I had five in here.
Then after I got the very first reading of location I would have to physically move five or more meetings
before the GPS system said Oh you need a new reading.
OK.
By putting it down to zero it's going to ping the location constantly like every update which is probably
a bit of overkill as well but when you're testing it you want to see it changing.
So I've got that at zero.
If you were to put these in some kind of application environment where you only had to move between
locations that were like 10 meters or 20 minutes a pop.
And of course you wouldn't have it pinging all the time at zero or you would sit that to a bit bigger
and that would then of course increase the battery life battery usage of the device that it's running
on.
All right so after that after you've started it we've then got a integer value for the maximum amount
of time that this method should wait until the location services has initialized.
And if it doesn't initialize within 5 seconds it's going to fail.
So this is just a little wallet that's going to stick around until the location status is initialized
there.
Oh you've run out of seconds to wait.
Now this I do believe if you have a look at the example code for these location services in the unity
documentation it it has something like 20 seconds.
When you're testing this for yourself 20 seconds is a long time to keep waiting.
Every time you set up these up.
So I've just got it sit down to five in a real world application you probably could have it at a bit
higher than that because usually what you're hoping for is that the location service is going to start
up before it gets any We need five seconds and you want it to be quite quick.
OK.
Now after that we've just got a little test to see if we timed out and whether we did run out of time
waiting.
And you can see that I've still got the old 20 seconds still sitting in there from the code from the
documentation.
So it is five now below that we testing if it failed.
And if it failed status location that status will be location service status dot filed the message on
the screen will then say unable to determine device location and again break us out of this method.
Now if we're at this point then we've made it.
We're actually going to get some data back from G.P.A..
Now I've got a line in here for enabling the compass which will look at a little bit light out so you
can put that a nephew like or revisit it later on.
But the important thing here is it my message string that I'm printing to the screen is now going to
print out all the data that's in important location the lost data which will be filled with out G.P.A.
information and that will have the latitude the longer cheered.
I'm also getting an altitude.
How high above sea level you are.
A horizontal accuracy and also vertical accuracy should you want that.
What we are really interested in at the moment is the longer chewed and the latitude and just a little
further down you'll say I'm also printing out the compass heading.
After that it is running an important location to stop.
So this application is querying the GP s once and then turning itself off and just printing out on the
screen where it thinks it is.
So it's not going to be something that's constantly updating for us which kind of makes those distance
of five and that time of zero back up here a little bit superfluous for now.
But we will start doing more pings on the service in a moment.
Once we've got this little bit working for ourselves the first now to run this car or train in the first
place we'll just go down to start and add in a line here start car retain start GPA.
And that will get this all running as soon as a press play or as soon as the application is running
on your mobile device.
Right.
So with all that that we can save it and now go back into unity and you want to build it out to your
mobile device.
And don't forget to say we could choose key.
And also you bundle I day as well as the other set up that you have to do before you attempt to build
at.
Now once that loads you should get a rating of your longer chewed and latitude and a few other details
as well.
But as I said the ones I'm really interested in your actual coordinates of where you are on the earth.
So if you're not sure what your long the chewed and latitude is and it's probably a good idea if you
head on over to Google maps and look up your location so you can check it against what you're getting
from the GPA in your device will now modify the code so that you can walk around with your device and
see the GPS location being updated in real time for you.
First you're going to need right at the top of your script using System.
And we need system because we're going to pull out the date and time a really handy feature of the location
service is that it will tell us when at last I asked the G.P.A. system for data because it doesn't get
it constantly.
And then you can check the time that it last checked against the current time and then see how old the
daughter is.
Because if you're moving around you might want to know whether you've got the most accurate up to date
position of where you are or whether you've got a position that was like five minutes ago.
Well not that long ago but you know what I mean.
And even when we do have that start with the delay of 0 in it then we still don't get it every single
moment and all the time is just when your device has turned up date and access that JPA system.
So the system here is going to get the time force up the top after message we're going to declare a
few more properties and that includes these things.
So first of all we have a float for this letter.
So this will just store the latitude and longitude which will obviously store the longer chewed from
the system.
We're also going to declare a date time called epoch.
Now generally speaking an epoch is a moment in time which other times measured from what we talk about
in computing as what the park is what's known as Unix time.
So in computing we measure time in seconds or have a time stamp in seconds from a particular date and
that date that pork is the first of January 19 70.
So the time stamp that we get back from G.P.A. system is actually the number of seconds since the first
of January 1970.
So by starting off with a date time called the Park we can then add on the number of seconds we get
back from the GP.
Convert it to a date time and then we know what time it was when the GP was lost queried.
So we're going to do this all down in sort of an update.
Now the way I've written this code I've ended up with my update at the bottom.
So let's go down and find it.
OK.
So in the update we'll just add in this first creating Date Time Code last update and last update is
the time when the JPR system was created last so it will be the Abe Hawk plus seconds of input location
last data time stamp that will hold the number of seconds for us then we want to get right now as well
so that we can print both times on the screen and then compare them for ourselves.
So right now is a date time and it's just equal to date time now.
And this will get the current time and you can use this any other time you want in unity or say shop
to get the current time and then display it.
Then we're going to set the latitude and longitude using the values we did before with Lost Data latitude
and lost out adult longer chewed those values then put into the message.
The message is now going to say current latitude is that current longer today is that the last update
time update time is last update to string.
Now with the date time you can give it a formatting string which is going to give you what it's going
to look like on the screen.
So in this case I'm printing out the time so the hours the minutes and the seconds.
You can also print out the day the month and the year as well.
But for us because we're updating this on the fly we're not really concerned about what the day is we
just want to look at the time difference.
So this is the updated japes time and this is the actual time on the device.
OK so once you've got that they also need to remember that it has a chain that we created just up a
little bit and you can still see it rodded the end.
We stop the location service.
Now if we do this heah these values here aren't going to update.
So let's just go up there and comment that out to stop it right so now you're ready to build this out
and go for a little wander around and see how those values change for you.
So back to unity and we will build in room once you've built that and I want you to take notice of what
the numbers are how they change as you move around and we'll come back in the next video and discuss
them.