It works by scanning different sources of location.
Global Positioning System (GPS) - is probably the most accurate method that it uses. It can pinpoint locations better than the rest of them.
Network signals - they can get through IP addresses, WiFi and Bluetooth connections.
GSM and cell IDs
User Input
So, these are all ways that Geolocation can get your position.
Now we want to check the browser, to see if there's geolocation support. Like I said, all the latest versions of every browser at this time do you support Geolocation. But you should still add this in your code, just in case some people still use older computers and older browsers. And the API offers a very handy function to detect for support. It's as simple as this:
if(navigator.geolocation) {
//do stuff
}
It's as simple as checking the navigator object for Geolocation property. You can also use Modernizr as we've used in past sections. It's just as simple as this function.
Now we will start looking at the code a little bit. Geolocation Code and API's. getCurrentPosition() is the main API or method, and if successful, it returns a position object. The accuracy, longitude, and latitude properties are always returned. Down here, we have the syntax.
getCurrentPosition(showLocation, ErrorHandler, options);
We have to getCurrentPosition() method and it takes three parameters -
showLocation which is mandatory and gets called if the position is obtained successfully and this method is specified by the success callback parameter
ErrorHandler method, which invokes where there is any kind of error or if the positioning fails
options parameter, which define a set of options that we can use
These are the getCurrentPosition's possible return values:
coords.latitude and coords.longitude as a decimal number
coords.accuracy` of the position
coords.altitude
coords.heading
coords.speed
timestamp for date/time of the response
So, once we successfully obtain the position of the user, we want to present it to them. And, we can do that in two ways. There's the Geodetic, and there is Civic representation. You'll see down here, each attribute has both a Geodetic and Civic representation, and the Civic is much more human-readable.
Attribute Geodetic Civic
Position 59.3, 18.6 Stockholm
Elevation 10 meters 4th floor
Heading 234 degrees To the city center
Speed 5 km/h Walking
Orientation 45 degrees NorthEast
As you can see, when using the Geolocation API, you get the geodetic data back from the functions, and you can present the location data in raw numbers. But that's not very useful and not very user-friendly. But you can use online services like Bing Maps and Yahoo Geo planet, and they can assist you to translate between the two presentation modes. It's a little complicated, little geeky, but we can go deeper when we get into the program. So, that's it for now!