>> SPEAKER: Let's implement a web page using a bit of HTML and JavaScript
that geolocates a user, that is, figures out where they are
geographically in terms of their latitude and longitude coordinates.
Notice here as an attribute on my body tag, I have onload, which specifies a
bit of JavaScript that should get executed as soon as the body of the
page has loaded.
Now that function appears to be called geolocate, And that function, I've
begun to implement, up top here in between script tags called geolocate.
>> Now let's implement this function.
First, let's check if the type of a special property called
navigator.geolocation does not equal, quote unquote, "undefined," then I'm
going to do the following.
Navigator.geoloc ation.getCurrentPosition.
And now I need to specify an argument, specifically the name of a function
that I'd like to be called when Get Current Position has figured out where
their user is.
And I'm just going to arbitrarily call that function for now, callback.
>> Else, if that property was in fact undefined, I'm going to specify
instead alert "Your browser does not support geolocation!"
and leave it at that.
So what is it that I typed here?
>> Well, it turns out that most modern web browsers support a global
variable, an object, so to speak, called navigator, that has
navigator-specific, that is browser-specific functionality.
Inside of there is a property called geolocation which specifically
contains information and functionality related to the geolocation, the
finding, of users.
>> Inside of that object, meanwhile, there appears to be a function,
otherwise known has a method, called Get Current Position.
And that's the function we're using in order to find a user.
>> Let's though now implement the callback function that's actually
going to get called when the user has been located.
Let's go ahead and declare that as well with function callback and let's
call the argument that I know it will be receiving say, position.
That is going to be, it turns out, an object that somehow represents the
user's position in the world.
>> Then inside of this function, let's output with alert position.coords,
which stands for coordinates, .latitude and then concatenate on to
that a comma and a space and then on to that, position.coords.longitude.
>> Let's go ahead and save this file, open the page in a browser, and see if
we can't find myself.
http://localhost /geolocation-0.html.
And there I am, in Cambridge, Massachusetts.