如何使用地理位置跟踪用户的实时位置? [英] How to track user live location using geolocation ?
问题描述
我有两点:
1)起点
2)目的地点
使用下面显示的函数绘制两点之间的路径,名为calculateAndDisplayRoute():
我想要做的是跟踪用户的实时位置标记以显示在绘制的路径上,以及如何在使用localhost时测试用户的实时位置。
任何帮助表示赞赏。
我的尝试:
函数calculateAndDisplayRoute(directionsService,directionsDisplay){
var dest;
if($(#pac-input)。val()!=){
dest = $(#pac-input)。val();
} else {dest = GlobalstartPoint}
directionsService.route({
origin:dest,
destination:document.getElementById('endPoint')。value,
optimizeWaypoints:true,
travelMode:'DRIVING'
},function(响应,状态){
console.log(响应)
console.log(状态)
if(status ==='OK' ){
directionsDisplay.setDirections(response);
// var route = response.routes [0];
} else {
window.alert(由于'+状态''路线请求失败);
}
});
}
I have a two points:
1) start point
2) destination point
To draw the path between the two points im doing so with the function shown below called calculateAndDisplayRoute():
What i want to do is track the user live location marker to be shown on the drawn path and also how can i test user live location when im using localhost.
Any help is appreciated.
What I have tried:
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var dest;
if ($("#pac-input").val() != "") {
dest = $("#pac-input").val();
} else { dest = GlobalstartPoint }
directionsService.route({
origin: dest,
destination: document.getElementById('endPoint').value,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function (response, status) {
console.log(response)
console.log(status)
if (status === 'OK') {
directionsDisplay.setDirections(response);
//var route = response.routes[0];
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
推荐答案
(#pac-input)。val()!=){
dest =
("#pac-input").val() != "") {
dest =
(#pac-input)。val();
} else {dest = GlobalstartPoint}
directionsService.route({
origin:dest,
destination:document.getElementById(' endPoint')。value,
optimizeWaypoints:true,
travelMode:'DRIVING'
},函数(响应,状态){
console.log(响应)
console.log(状态)
if(status ==='OK'){
directionsDisplay.setDirections(response);
// var route = response.routes [0];
} else {
window .alert('由于'+状态而导致请求失败);
}
});
}
("#pac-input").val();
} else { dest = GlobalstartPoint }
directionsService.route({
origin: dest,
destination: document.getElementById('endPoint').value,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function (response, status) {
console.log(response)
console.log(status)
if (status === 'OK') {
directionsDisplay.setDirections(response);
//var route = response.routes[0];
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
这篇关于如何使用地理位置跟踪用户的实时位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!