Tap Forms Database Pro for Mac, iPhone, iPad and Apple Watch › Forums › Script Talk › How to set Location through script
- This topic has 5 replies, 2 voices, and was last updated 4 months, 2 weeks ago by
Tap forms Forum.
-
AuthorPosts
-
June 12, 2025 at 3:06 AM #52446
Tap forms ForumParticipantHow do I save to a location field?
For a test I tried getting the value from a filled in location field and setting a test-location field to it, but that doesn’t work. Is this a bug, or am I using the wrong commands?
function Test_Script() { var scouting_location = record.getFieldValue('fld-b625c89aa5664db4933aa9ecc8bedcde'); var temp_location_id = 'fld-6b6551dc87e9420aaedb2887d525cff3'; console.log (scouting_location); record.setFieldValue(temp_location_id, scouting_location); } Test_Script();In the end I’m hoping to use it to retrieve a location of a photo:
function Scouting_Script() { var locationFieldID = form.getFieldNamed('Scouting Location').getId(); var photoFieldID = form.getFieldNamed('Scouting Photo').getId(); var media = record.fetchMediaForField(photoFieldID); if (!media || media.length === 0) { return "No photo found"; } var gpsData = media[0].getGpsData(); if (gpsData && gpsData.Latitude && gpsData.Longitude) { var lat = gpsData.Latitude; var lon = gpsData.Longitude; var locationString = lat + " " + lon; record.setFieldValue(locationFieldID, locationString); document.saveAllChangesAndRefresh(); return locationString; } else { return "No GPS data found in photo"; } } Scouting_Script();June 12, 2025 at 9:15 PM #52453
BrendanKeymasterYou should fetch the location field value and then just set the latitude and longitude on it:
let locationValue = record.getFieldValue(locationFieldID); locationValue.longitude = lon; locationValue.latitude = lat;There’s also
locationTitleandlocationSubTitlefields on a Location field value. ThelocationSubTitlefield ends up displaying in the Address field. And I just realized that I will be renaming that tolocationAddressin the next build.June 13, 2025 at 12:24 PM #52457
Tap forms ForumParticipantThanks for your reply Brendan!
Unfortunately I still don’t get it…
I now wrote this (with the help of ChatGPT), but it still doesn’t work. Any pointers would be greatly appreciated
function Copy_Location_Script() { var source_location_id = form.getFieldNamed("Source Location").getId(); var target_location_id = form.getFieldNamed("Target Location").getId(); let sourceValue = record.getFieldValue(source_location_id); if (!sourceValue || !sourceValue.latitude || !sourceValue.longitude) { return "Source location is not set."; } let lon = sourceValue.longitude; let lat = sourceValue.latitude; // Fetch current value of target field let locationValue = record.getFieldValue(target_location_id); locationValue.longitude = lon; locationValue.latitude = lat; // Save updated value back to record record.setFieldValue(target_location_id, locationValue); // Return debug info return "Target location set to: " + JSON.stringify(record.getFieldValue(target_location_id)); } Copy_Location_Script();June 13, 2025 at 11:50 PM #52462
BrendanKeymasterAh whoops. Just looked into my code and you need to use
locationValue.lon = lonandlocationValue.lat = latBut I just realized that’s silly because I have reading the value with
latitudeandlongitude, but writing the value aslatandlon. I’ve just fixed that for the next update so it’ll belatitudeandlongitudefor reading and writing.So just try
latandlonfor now.June 13, 2025 at 11:51 PM #52463
BrendanKeymasterAnd don’t forget to call
form.saveAllChanges();to commit the value to the database.June 14, 2025 at 2:45 AM #52467
Tap forms ForumParticipantAhhhh Thank you!
-
AuthorPosts
You must be logged in to reply to this topic.