Javascript 理解地理定位示例
我正在看一个地理位置示例,该示例为用户提供了从地理位置到柏林Alexanderplatz的方向,但我很难理解两个独立的后备方案:Javascript 理解地理定位示例,javascript,html,geolocation,google-api,Javascript,Html,Geolocation,Google Api,我正在看一个地理位置示例,该示例为用户提供了从地理位置到柏林Alexanderplatz的方向,但我很难理解两个独立的后备方案: function () { // Gelocation fallback: Defaults to Stockholm, Sweden createMap({ coords : false, address :
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>
(功能(){
var directionsService=new google.maps.directionsService(),
directionsDisplay=新建google.maps.DirectionsRenderer(),
createMap=函数(开始){
var行程={
来源:(start.coords)?新的google.maps.LatLng(start.lat,start.lng):start.address,
目的地:“柏林亚历山大广场”,
travelMode:google.maps.Directions travelMode.DRIVING
//把开车换成在上面走会很有趣:-)
},
映射选项={
缩放:10,
//默认视图:斯德哥尔摩市中心
中心:新google.maps.LatLng(59.3325215,18.0643818),
mapTypeId:google.maps.mapTypeId.ROADMAP
};
map=new google.maps.map(document.getElementById(“map”)、mapOptions);
方向显示.setMap(地图);
directionsDisplay.setPanel(document.getElementById(“地图方向”);
方向服务.路线(行程,功能(结果,状态){
if(status==google.maps.directionstatus.OK){
方向显示。设置方向(结果);
}
});
};
//检查地理定位支持
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(函数(位置){
//成功!
创建地图({
库德:没错,
纬度:位置坐标纬度,
lng:position.coords.longitude
});
},
函数(){
//Gelocation备用:默认为瑞典斯德哥尔摩
创建地图({
coords:错,
地址:“斯德哥尔摩斯维亚夫根”
});
}
);
}
否则{
//无地理位置回退:默认为葡萄牙里斯本
创建地图({
coords:错,
地址:“葡萄牙里斯本”
});
}
})();
代码将首先检查浏览器是否支持:
// Check for geolocation support
if (navigator.geolocation) {
如果浏览器不支持该新API,则else
分支将地图地址设置为葡萄牙里斯本:
但是如果浏览器提供地理定位API,代码将尝试获取当前位置。有可能检索失败,例如,如果用户不允许使用其位置。 然后地图的地址将设置为斯德哥尔摩的Sveavägen
我只是想找人解释为什么在第一行代码中有两个回退选项?我没有接受这个答案,因为它对我的问题没有帮助。从我的个人资料中可以看出,我接受所有有助于解决我问题的答案。
// else branch of geolocation check
else {
// No geolocation fallback: Defaults to Lisbon, Portugal
createMap({
coords : false,
address : "Lisbon, Portugal"
});
}
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!
}
);