hi
how to get latitude, longitude from database for dynamic data in database table kantor
CREATE TABLE Kantor ( Id INT PRIMARY KEY, Latitude FLOAT, Longitude FLOAT );
Latitude = -6.1960523
Longitude = 106.6962248
two variable have to take from database
var kantorLat = -6.1960523;
var kantorLon = 106.6962248;
html
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="absenkaryawan.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Absensi Karyawan</title>
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
<style>
#map {
height: 500px;
width: 100%;
}
</style>
</head>
<body>
<h3>Peta Kantor</h3>
<div id="map"></div>
<!-- Form Absensi -->
<div>
<button onclick="ambilLokasi()">Absensi</button>
</div>
<script>
var kantorLat = -6.1960523;
var kantorLon = 106.6962248;
var map = L.map('map').setView([kantorLat, kantorLon], 16);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var marker = L.marker([kantorLat, kantorLon]).addTo(map);
marker.bindPopup("<b>Kantor</b><br>Lokasi Kantor <br><b>Datanesia Soft.</b>").openPopup();
// Fungsi untuk mendapatkan lokasi karyawan (dari GPS perangkat)
function ambilLokasi() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function (position) {
var karyawanLat = position.coords.latitude;
var karyawanLon = position.coords.longitude;
alert("Koordinat Anda: " + karyawanLat + ", " + karyawanLon);
// Kirim data ke server untuk mengecek absensi
var xhr = new XMLHttpRequest();
xhr.open("POST", "WebForm1.aspx/AbsenKaryawan", true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4 && xhr.status == 200) {
//alert("Absensi berhasil");
}
};
var data = JSON.stringify({
"latKaryawan": karyawanLat,
"lonKaryawan": karyawanLon
});
xhr.send(data);
}, function () {
alert("Gagal mendapatkan lokasi.");
});
} else {
alert("Geolocation tidak didukung di browser ini.");
}
}
</script>
</body>
</html>
Imports System.Web.Services
Imports System.Data.SqlClient
Public Class WebForm1
Inherits System.Web.UI.Page
Dim kantorLat As Double = -6.1960523
Dim kantorLon As Double = 106.6962248
Public Function GetDistance(lat1 As Double, lon1 As Double, lat2 As Double, lon2 As Double) As Double
Dim R As Double = 6371000
Dim phi1 As Double = lat1 * Math.PI / 180
Dim phi2 As Double = lat2 * Math.PI / 180
Dim deltaPhi As Double = (lat2 - lat1) * Math.PI / 180
Dim deltaLambda As Double = (lon2 - lon1) * Math.PI / 180
Dim a As Double = Math.Sin(deltaPhi / 2) * Math.Sin(deltaPhi / 2) +
Math.Cos(phi1) * Math.Cos(phi2) *
Math.Sin(deltaLambda / 2) * Math.Sin(deltaLambda / 2)
Dim c As Double = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a))
Return R * c ' Hasil dalam meter
End Function
<WebMethod()>
Public Shared Sub AbsenKaryawan(latKaryawan As Double, lonKaryawan As Double)
Dim absensi As New WebForm1()
Dim distance As Double = absensi.GetDistance(latKaryawan, lonKaryawan, absensi.kantorLat, absensi.kantorLon)
'Debug.WriteLine("Jarak Karyawan ke Kantor: " & distance & " meter")
If distance <= 100 Then
Dim karyawanId As Integer = 1 ' Id Karyawan (dapat diambil sesuai dengan data login karyawan)
Dim waktuAbsen As DateTime = DateTime.Now
SimpanAbsen(karyawanId, waktuAbsen)
Else
' Jika jarak lebih dari 100 meter, beri notifikasi
Debug.WriteLine("Karyawan tidak berada dalam radius 100 meter dari kantor.")
End If
End Sub
' Fungsi untuk menyimpan data absen ke database
Private Shared Sub SimpanAbsen(karyawanId As Integer, waktuAbsen As DateTime)
MsgBox("Absensi berhasil data Di Simpan ke Database: " & karyawanId & "-" & "Jam: " & waktuAbsen)
End Sub
End Class
variable kantorLat and kantorLon have to get from database
Dim kantorLat As Double = -6.1960523
Dim kantorLon As Double = 106.6962248
thanks