首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >了解地理位置示例

了解地理位置示例
EN

Stack Overflow用户
提问于 2012-11-15 01:19:12
回答 1查看 605关注 0票数 0

我正在看一个地理位置示例,它为用户提供了从他们的地理位置到Alexanderplatz,柏林的方向,但我无法理解这两个独立的后备:

代码语言:javascript
复制
        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"
        });

下面是完整的代码:

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-16 03:31:42

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

代码语言:javascript
复制
// Check for geolocation support    
if (navigator.geolocation) {

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

代码语言:javascript
复制
// else branch of geolocation check
else {
  // No geolocation fallback: Defaults to Lisbon, Portugal
  createMap({
    coords : false,
    address : "Lisbon, Portugal"
  });
}

如果浏览器提供了Geolocation API,代码将尝试获取当前位置。

存在检索失败的可能性,例如,如果用户不允许使用他的位置。然后,地图的地址将设置为斯德哥尔摩的Sveavägen。

代码语言:javascript
复制
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!
  }
);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13383852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档