1
我用Google Map写了一段代码来实现位置和GPS功能。 但导航路线总是无法显示,我不知道哪里出错了? 寻求帮助。谁能帮助我? 谢谢!Google Map API麻烦,请教支持
JavaScript代码
<script type="text/javascript">
$("#map-page").live("pageinit", function() {
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var salon = new google.maps.LatLng(22.981666,120.194301);
var defaultLatLng = new google.maps.LatLng(22.983587,120.22599); // Default to Hollywood, CA when no geolocation support
if (navigator.geolocation) {
function success(pos) {
// Location found, show map with these coordinates
drawMap(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
calcRoute(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
}
function fail(error) {
console.log(error);
drawMap(defaultLatLng); // Failed to find location, show default map
}
// Find the users current position. Cache the location for 5 minutes, timeout after 6 seconds
navigator.geolocation.getCurrentPosition(success, fail, {maximumAge: 500000, enableHighAccuracy:true, timeout: 6000});
} else {
drawMap(defaultLatLng); // No geolocation support, show default map
}
function drawMap(latlng) {
directionsDisplay = new google.maps.DirectionsRenderer();
var myOptions = {
zoom: 10,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
directionsDisplay.setMap(map);
// Add an overlay to the map of current lat/lng
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "Greetings!"
});
var marker = new google.maps.Marker({
position:new google.maps.LatLng(22.981666,120.194301),
map:map,
title:"the salon"
});
}
function calcRoute(latlng) {
var start = latlng;
var end = new google.maps.LatLng(22.981666,120.194301);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
});
</script>
我希望你有'传感器= TRUE'? – mast0r
关于您的代码的注意事项 - 您的评论阅读 - “查找用户当前位置,缓存位置5分钟,6秒后超时” - 但您的代码显示为:navigator.geolocation.getCurrentPosition(success,fail,{maximumAge :500000,enableHighAccuracy:true,timeout:6000});有两个问题。 500000是8.33分钟,而不是5分钟(500000/1000/60)。但是,更重要的是,现在这是最大年龄的含义。您正在告诉设备您愿意接受的位置年龄,但告诉它需要多长时间进行缓存。 –