可以通过以下步骤实现:
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
// 将经纬度值插入数据库的逻辑
});
} else {
console.log("Geolocation is not supported by this browser.");
}
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'your_username',
password: 'your_password',
database: 'your_database'
});
connection.connect();
// 假设有一个名为locations的表,包含latitude和longitude两个字段
const latitude = 37.7749;
const longitude = -122.4194;
const sql = `INSERT INTO locations (latitude, longitude) VALUES (${latitude}, ${longitude})`;
connection.query(sql, function(error, results, fields) {
if (error) throw error;
console.log('Location inserted successfully.');
});
connection.end();
CREATE TABLE locations (
id INT AUTO_INCREMENT PRIMARY KEY,
latitude DECIMAL(9,6),
longitude DECIMAL(9,6)
);
领取专属 10元无门槛券
手把手带您无忧上云