了解地理位置示例 [英] Understanding geolocation example
问题描述
我正在寻找一个地理定位示例,该地理定位示例向用户指示柏林亚历山大广场的地理位置,但我无法理解这两个单独的回退:
<$ p $ (
coords:false,
address:Sveavägen,p。 function(){
// Gelocation fallback:默认为瑞典斯德哥尔摩
createMap斯德哥尔摩
});
}
);
}
else {
//无地理位置后备:默认为葡萄牙里斯本
createMap({
coords:false,
address:葡萄牙
);
以下是完整的代码:
< 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,柏林,
travelMode:google.maps.DirectionsTravelMode.DRIVING
//上面交换DRIVING到WALKING可以证明相当有趣:-)
},
mapOptions = {
zoom:10,
//默认视图:斯德哥尔摩市中心
中心:新增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);
}
});
};
//检查地理定位支持
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(function(position){
// Success!
createMap({
coords:true,
lat:position.coords.latitude,
lng:position.coords.longitude
});
},
功能(){
// Gelocation后退:默认为瑞典斯德哥尔摩
createMap({
coords:false,
地址:斯德哥尔摩Sveavägen
});
}
);
}
else {
//无地理位置后备:默认为葡萄牙里斯本
createMap({
coords:false,
address:Lisbon,葡萄牙
);
}
})();
< / script>
代码将检查支持浏览器的地理定位:
//检查地理位置支持
if(navigator.geolocation){
如果浏览器不支持新的API, else
分支会将地图的地址设置为葡萄牙里斯本:
// else地理定位检查
else {
//无地理位置后退:默认为葡萄牙里斯本
createMap( {
coords:false,
地址:葡萄牙里斯本
});
}
但如果浏览器确实提供了Geolocation API ,代码将尝试获取当前位置。
有可能出现回复失败的情况,例如,如果用户不允许使用他的位置。
然后,地图的地址将被设置为Sveavägen,Stockholm 。
navigator。 geolocation.getCurrentPosition(
function(position){
//这是成功函数:存储位置的位置!
},
function(){
//这是'失败'功能:位置不能被检索!
}
);
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>
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!
}
);
这篇关于了解地理位置示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!