Skip to content Skip to sidebar Skip to footer

Understanding Geolocation Example

I am looking at a geolocation example which gives a user directions to Alexanderplatz, Berlin from their geolocation but I'm having trouble understanding the two separate fallbacks

Solution 1:

The code will check for Geolocation support of the browser at first:

// Check for geolocation support    
if (navigator.geolocation) {

If the browser doesn't support that new API, the else branch will set the maps' address to Lisbon, Portugal:

// else branch of geolocation checkelse {
  // No geolocation fallback: Defaults to Lisbon, PortugalcreateMap({
    coords : false,
    address : "Lisbon, Portugal"
  });
}

But if the browser does offer the Geolocation API, the code will try to get the current position. There are possibilities where the retreival fails, for example if the user doesn't allow using his location. Then the maps' address will be set to Sveavägen, Stockholm.

navigator.geolocation.getCurrentPosition(
  function (position) {
    // This is the success function: location stored in position!
  },

  function () {
    // This is the 'fail' function: location could not be retreived!
  }
);

Post a Comment for "Understanding Geolocation Example"