了解地理位置示例

时间:2012-11-14 17:19:12

标签: javascript html geolocation google-api

我正在寻找一个地理位置示例,它为用户提供了从地理位置到柏林亚历山大广场的路线,但我无法理解这两个单独的后备:

        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 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 个答案:

答案 0 :(得分:1)

代码首先会检查浏览器的Geolocation支持:

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

如果浏览器不支持该新API,else分支会将地图的地址设置为里斯本,葡萄牙

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

如果浏览器确实提供了Geolocation API,代码将尝试获取当前位置。
存在撤销失败的可能性,例如,如果用户不允许使用他的位置。 然后地图的地址将设置为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!
  }
);