Understanding geolocation example

£可爱£侵袭症+ 提交于 2019-12-11 19:04:45

问题


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:

        function () {
                // Gelocation fallback: Defaults to Stockholm, Sweden
                createMap({
                    coords : false,
                    address : "Sveavägen, Stockholm"
                });
            }
        );
    }
    else {
        // No geolocation fallback: Defaults to Lisbon, Portugal
        createMap({
            coords : false,
            address : "Lisbon, Portugal"
        });

Here is the full code:

<script src="http://maps.google.se/maps/api/js?sensor=false"></script>
<script>
    (function () {
        var directionsService = new google.maps.DirectionsService(),
            directionsDisplay = new google.maps.DirectionsRenderer(),
            createMap = function (start) {
                var travel = {
                        origin : (start.coords)? new google.maps.LatLng(start.lat, start.lng) : start.address,
                        destination : "Alexanderplatz, Berlin",
                        travelMode : google.maps.DirectionsTravelMode.DRIVING
                        // Exchanging DRIVING to WALKING above can prove quite amusing :-)
                    },
                    mapOptions = {
                        zoom: 10,
                        // Default view: downtown Stockholm
                        center : new google.maps.LatLng(59.3325215, 18.0643818),
                        mapTypeId: google.maps.MapTypeId.ROADMAP
                    };

                map = new google.maps.Map(document.getElementById("map"), mapOptions);
                directionsDisplay.setMap(map);
                directionsDisplay.setPanel(document.getElementById("map-directions"));
                directionsService.route(travel, function(result, status) {
                    if (status === google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(result);
                    }
                });
            };

            // Check for geolocation support    
            if (navigator.geolocation) {
                navigator.geolocation.getCurrentPosition(function (position) {
                        // Success!
                        createMap({
                            coords : true,
                            lat : position.coords.latitude,
                            lng : position.coords.longitude
                        });
                    }, 
                    function () {
                        // Gelocation fallback: Defaults to Stockholm, Sweden
                        createMap({
                            coords : false,
                            address : "Sveavägen, Stockholm"
                        });
                    }
                );
            }
            else {
                // No geolocation fallback: Defaults to Lisbon, Portugal
                createMap({
                    coords : false,
                    address : "Lisbon, Portugal"
                });
            }
    })();
</script>

回答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 check
else {
  // No geolocation fallback: Defaults to Lisbon, Portugal
  createMap({
    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!
  }
);


来源:https://stackoverflow.com/questions/13383852/understanding-geolocation-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!