GPS coordinates

The recommended method of customizing your AppGini-generated application is through hooks. But sometimes you might need to add functionality not accessible through hooks. You can discuss this here.
Post Reply
AEmpeno
Veteran Member
Posts: 72
Joined: 2018-01-04 18:48

GPS coordinates

Post by AEmpeno » 2021-04-12 04:47

I have a table, map, and a field name, location. What I would like to happen is every time I hit the save button, I would like the GPS coordinates (latitude and longitude) automatically save the coordinates to the field name.

Has anyone in here made this feature to their project?

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: GPS coordinates

Post by jsetzer » 2021-04-12 06:56

How to get GEO coordinates

Pure browser based solution (Javascript/JQuery). Use it for example in hooks/TABLENAME-dv.js in document-ready function:

Code: Select all


// on load or before save, use the following code:

navigator.geolocation.getCurrentPosition(function(position) {

  var lat = position.coords.latitude;
  var lng = position.coords.longitude;
  
  // for example put latitude and longitude into your fields like so:
  // $j("#geo_lat").val(lat);
  // $j("#geo_lng").val(lng);
  
});
Important:
See limitations and notes somewhere below!


How to check browser compatibility

The code will only work in compatible browsers. You should check this before:

Code: Select all

 if (navigator.geolocation) {
   // yes, the user's browser supports geolocation API
   // execute your code here
  } else {
  // no, the user's browser does not support this
  }
  

How to prepare storage of GEO coordinates

I am using two fields for storing latitude and longitude with high precision:

AppGini_3rBmWYk7W7.png
AppGini_3rBmWYk7W7.png (66.43 KiB) Viewed 1696 times


Technical notes and limitations

Due to GPS technology, internet-technology, various browser differences and privacy concerns there are some known limits.
  • This is not 100% accurate
  • Especially when using from a desktop PC the calculated coordinates may be 100 miles away and show the the location of your ISP
  • Requires HTML5 GEO location API
  • This means, it will not work on all devices
  • Browser will ask user for permission
  • Depending on the browser and browser-version, there may be limitations when using insecure context (HTTP instead of HTTPS) outside localhost.
Legal notes
  • Depending on the law and regulations in the country of your users it may be illegal to store and/or track a user's position at a certain time without their permission due to privacy concerns
  • If the users are your employees, then you can have them sign agreements
  • If they are any internet users, then at least a consent form will be required when registering
Example

This is a DV in one of my projects:

chrome_D132VpjxCp.png
chrome_D132VpjxCp.png (27.77 KiB) Viewed 1696 times

Further usage of GEO coordinates

As soon as you have those coordinates you can use leaftlet.js (https://leafletjs.com/) for example for building beautiful maps.

I prefer leaflet.js over Google maps, because giving sensitive tracking data to Google is a NO-GO for me and my customers.

partners-map-800x600.jpg
partners-map-800x600.jpg (231.13 KiB) Viewed 1696 times
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: GPS coordinates

Post by jsetzer » 2021-04-12 07:27

Annotation*

I recommend storing Lat/Long in two separate fields instead of one concatenated location-field.

Storing them in one concatenated location-field is fine in case you just want to store it. But you cannot really work with that data unless you split the string into two parts and convert them back into numeric values each time using them.

Having them separately will help you when really making use of the stored data. For example, if, later on, you need calculations like "give me all records within a distance of 100km around x,y" it will be much easier using the database's GEO spatial functions "as is" without splitting/converting location into lat/long, first.

As a personal, general rule, I try to keep detailed, atomic information "as is" (which means separated and using correct datatype). I avoid concatenating details into strings or blobs because I know, later on I will have to split that again and convert it back. For me, concatenating does not give any benefit but decreases performance, requires more diskspace (most of the times) and makes things harder to code, for example when fetching records using SQL or splitting/parsing in PHP/Javacript later on. Of course, there are exceptions to this personal rule. But this is how I generally proceed.

This is just my personal preference and experience. Of cause it depends on your future perspective and requirements.


* I wasn't able to append to my previous post, so I had to create a new post
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

AEmpeno
Veteran Member
Posts: 72
Joined: 2018-01-04 18:48

Re: GPS coordinates

Post by AEmpeno » 2021-04-19 19:12

Thanks, jsezer! It works!

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: GPS coordinates

Post by jsetzer » 2021-04-19 20:22

👍🏻
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

Post Reply