问题
Is there a way to get my current position (latitudes and longitudes, preferably in decimal form) from my phone's GPS data with a Google Apps Script? Also, is it possible to turn GPS on and off or at least detect if it's on or off?
Here's what I try to do: I go to places with my electric moped, and at every place I enter some data in a spreadsheet, such as date, distance since last position, which battery I'm using and more. One of the columns is my position. I currently enter it manually, but some positions are very common (for instance ”Work” and ”Home”) and it would be convenient if a script entered my position for me on edit.
I do that already, but it's very primitive at the moment. The only thing it does now, is check if it's a working day, and in that case, if my last position was ”Home”, it will enter ”Work”, and ”Home” if my last position was ”Work”. That takes care of the most common situations, but there are a few more places I go to often, so I figured that if I can get my current GPS coordinates from my phone, I could use them for figuring out if I'm at work, at home, or at one of the grocery stores I visit often or whatever.
I have searched for the answer, but all I can find is how to convert addresses to coordinates and the other way around, which obviously is not what I'm looking for at all.
回答1:
I think we can try Geolocation API
Something like this (be mindful about handling user consent):
navigator.geolocation.getCurrentPosition(function(location) {
console.log(location.coords.latitude);
console.log(location.coords.longitude);
console.log(location.coords.accuracy);
});
https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API
Credits : Get GPS location from the web browser
回答2:
Google Apps Script doesn't include a direct way the GPS data but this is possible by creating a Web App and using the Geolocation API.
Example:
I adapted the code frond on the above link to
- Use it in a Google Apps Script web application
- Log the latitude and longitud into a spreadsheet
Code.gs
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index');
}
function saveCoordinates(latitude,longitude){
const sheet = SpreadsheetApp.getActiveSheet();
const rowContents = [latitude,longitude];
sheet.appendRow(rowContents);
}
index.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<button id = "find-me">Show my location</button><br/>
<p id = "status"></p>
<a id = "map-link" target="_blank"></a>
<script>
function geoFindMe() {
const status = document.querySelector('#status');
const mapLink = document.querySelector('#map-link');
mapLink.href = '';
mapLink.textContent = '';
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
google.script.run.saveCoordinates(latitude,longitude);
status.textContent = '';
mapLink.href = 'https://www.openstreetmap.org/#map=18/' + `${latitude}\/${longitude}`;
mapLink.textContent = `Latitude: ${latitude} °, Longitude: ${longitude} °`;
}
function error() {
status.textContent = 'Unable to retrieve your location';
}
if(!navigator.geolocation) {
status.textContent = 'Geolocation is not supported by your browser';
} else {
status.textContent = 'Locating…';
navigator.geolocation.getCurrentPosition(success, error);
}
}
document.querySelector('#find-me').addEventListener('click', geoFindMe);
</script>
</body>
</html>
To use the above in your phone, you could add the link to the web application into your spreadsheet, so it can be openened on the phone web browser. In order to make this work you need to have an Internet connection enabled.
来源:https://stackoverflow.com/questions/59808246/getting-my-current-position-from-my-phone-with-script-in-google-spreadsheet