0

地理位置情報からベルリンのアレクサンダー広場への道順をユーザーに提供する地理位置情報の例を見ていますが、2 つの別々のフォールバックを理解するのに苦労しています。

        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>
4

1 に答える 1

3

このコードは、最初にブラウザーの地理位置情報サポートを確認します。

// 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!
  }
);
于 2012-11-15T19:31:42.140 に答える