如果地理位置被禁用,地圖應居中指定位置,但禁用時不會加載任何內容。啓用地理位置後,if
語句的第一部分正常工作。爲什麼else
零件在禁用時不工作?當地理位置被禁用時Google地圖不在位置上居中位置
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var coords = new google.maps.LatLng(latitude, longitude);
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var mapOptions =
{
zoom: 15,
center: coords,
mapTypeControl: true,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById(''));
var request = {
origin: coords,
destination: 'BT42 1FL',
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
});
}
else {
alert("Geolocation API is not supported in your browser.");
var mapOptions =
{
zoom: 15,
center: 'BT42 1FL',
mapTypeControl: true,
navigationControlOptions:
{
style: google.maps.NavigationControlStyle.SMALL
},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
}
alert
也不警告。
檢查navigator.geolocation的值,它是否被正確設置? – QuentinUK 2013-02-11 02:31:39
我該如何檢查? – Colin747 2013-02-11 02:33:37
難道你沒有調試器嗎? – QuentinUK 2013-02-11 02:37:59