|
|
|
ต้องการ หน่วงการรับพิกัด Geolocation wacthposition รบกวนดูโคดให้หน่อยครับ |
|
|
|
|
|
|
|
Code (JavaScript)
function gps()
{
var optionGps = {enableHighAccuracy: true,frequency: 3000};
wacthID = navigator.geolocation.watchPosition(successGps,showError,optionGps);
}
function successGps(position)
{
var lastPos = coords[coords.length-1];
if(lastPos)
{
distance += calculateDistance(lastPos,position.coords);
}
coords.push(position.coords);
document.getElementById("outGps").innerHTML = "lat:" + position.coords.latitude +"<br>"+"lon:" + position.coords.longitude +"<br>"+ "distance:" + distance.toFixed(2);
tracking(position);
}
function calculateDistance(fromPos, toPos) {
var radius = 6371;
var toRad = function(number) {
return number * Math.PI / 180;
};
var latDistance = toRad(toPos.latitude - fromPos.latitude);
var lonDistance = toRad(toPos.longitude - fromPos.longitude);
var a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
Math.cos(toRad(fromPos.latitude)) * Math.cos(toRad(toPos.latitude)) *
Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
return radius * (2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)));
}
function tracking(position) {
path.push(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
var polyline = new google.maps.Polyline({
map: map,
path: path,
strokeColor: '#0000FF',
strokeOpacity: 1.7,
strokeWeight: 10
});
map.setZoom(18);
map.setCenter(new google.maps.LatLng(position.coords.latitude, position.coords.longitude));
}
function showError(error)
{
alert("Error occurred. Error code: " + error.code);
// error.code can be:
// 0: unknown error
// 1: permission denied
// 2: position unavailable (error response from locaton provider)
// 3: timed out
}
<body onload="gps()">
<p id="showtime"</p>
<p id="btt">
<div><button id="Outbtt" onClick="timee()">Start</button> </div><br><br>
<div><button id="stopp" onClick="stopp()">Stop</button> </div>
</p>
<div id="googleMap" style="width:100%;height:400px;"></div>
<div id="outGps"</div>
</body>
</html>
Tag : Android
|
|
|
|
|
|
Date :
2015-12-07 20:36:17 |
By :
mepilutt |
View :
723 |
Reply :
1 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var optionGps = {enableHighAccuracy: true,frequency: 3000}; ผมใส่ frequency: 3000 ลงไปใน option แล้วแต่มันก็ยังรับพิกัดทุกวินาทีอยู่ดี ผมจะแก้ไขให้มันรับค่าพิกัด ทุกๆ 3 วิ แต่Gps ยังทำงานอยู่ตลอดเวลา ได้ยังไงครับ ขอบคุณครับ
|
|
|
|
|
Date :
2015-12-07 20:38:32 |
By :
mepilutt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|