Geolocation with FileMaker Go

UPDATE 4: Geolocation is now natively supported in FileMaker Go 12 with the new mobile functions Location() and LocationValues().  Yay! For FileMaker Go 11, read on…

Elizabeth in San Francisco is one of many to ask this interesting FileMaker Go question:

Is there any way to get the latitude and longitude of the phone in FileMaker Go and store them in a database?

My first reaction was “unfortunately, no.”  FileMaker Go can’t access the GPS features of the iPhone, and since we can’t use plug-ins with Go, there’s no opportunity to add that feature.  But never say never, right? 

After a few more moments thought, the answer appeared: FileMaker can’t access the GPS… but Mobile Safari can… and FileMaker has the web viewer.  Ah, the web viewer—FileMaker’s Swiss Army knife—how we love you!

Here’s our solution, requiring one web viewer, one script (plus a few more for fun), and just a few fields.

UPDATE 3: The following HTML code has worked the best:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">

            function handler(location) {
                var messageArea = document.getElementById("messageArea");
                messageArea.innerHTML="<p>Latitude: " + location.coords.latitude + "</p>";
                messageArea.innerHTML+="<p>Longitude: " + location.coords.longitude + "</p>";
                messageArea.innerHTML+="<p>Accuracy: " + location.coords.accuracy + "</p>";
            }
            
            function fail() { 
                var messageArea = document.getElementById("messageArea");
                messageArea.innerHTML="<p>Can't get located</p>"
            }
            
            function getLocation() {
                navigator.geolocation.watchPosition(handler,fail,{maximumAge: 5000, enableHighAccuracy: true, timeout: 30000});
            }
            
        </script> 
    </head>

    <body onload="getLocation();">
        <div id="messageArea">
            Location unknown 
        </div>
    </body>

</html>


UPDATE 2:

FileMaker Go 1.2.2 has been released, fixing the bug with GetLayoutObjectAttribute ( “web” ; “content” ).  Everything should be working again.

UPDATE:

Currently, this has been broken by FileMaker Go 1.2.1.  There’s a bug when using the GetLayoutObjectAttribute function to get a web viewer’s content when the web viewer uses a data URI. Although the web viewer will display the correct result, GetLayoutObjectAttribute ( “web” ; “content” ) always returns <html><head></head><body></body></html>, even when the HTML contains neither an html, head, or body element. FileMaker is aware of the issue, and I hope we see a fix soon.

I created a simple webpage that uses the HTML5 geolocation features used in Safari.

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">

            function handler(location) {
                var message = document.getElementById("messageArea");
                messageArea.innerHTML="<p>Latitude: " + location.coords.latitude + "</p>";
                messageArea.innerHTML+="<p>Longitude: " + location.coords.longitude + "</p>";
                messageArea.innerHTML+="<p>Accuracy: " + location.coords.accuracy + "</p>";
            }
        
            function getLocation() {
                navigator.geolocation.getCurrentPosition(handler);
            }
            
        </script> 
    </head>

    <body onload="getLocation();">
        <div id="messageArea">
            Location unknown 
        </div>
    </body>

</html>

With a browser that supports HTML5 geolocation services, when the location is detected the the result of that page should something like:

Latitude: 42.3028843
Longitude: -71.3770951
Accuracy: 30

with the source code now appearing as:

<!DOCTYPE HTML>
<html>
    <head>
        <script type="text/javascript">

            function handler(location) {
                var message = document.getElementById("messageArea");
                messageArea.innerHTML="<p>Latitude: " + location.coords.latitude + "</p>";
                messageArea.innerHTML+="<p>Longitude: " + location.coords.longitude + "</p>";
                messageArea.innerHTML+="<p>Accuracy: " + location.coords.accuracy + "</p>";
            }
        
            function getLocation() {
                navigator.geolocation.getCurrentPosition(handler);
            }
            
        </script> 
    </head>

    <body onload="getLocation();">
        <div id="messageArea"><p>Latitude: 42.3028843</p><p>Longitude: -71.3770951</p><p>Accuracy: 30</p></div>
    </body>

</html>

Notice the change following <div id=“messageArea”>. Depending on what browser you use to test this, viewing the source may or may not show you the alteration in the messageArea div.  With Safari, use the Web Inspector’s Elements tab, rather than View Source; with Firefox and the Web Developer extension, use the View Generated Source option in the toolbar.

UPDATE: It appears from the comments that the web viewer behaves just a tad differently with iOS 3.2 for iPad than with iOS 4 for iPhones and iPod touches, as far as we can see.  I made a small change to the HTML page to force the geolocation JavaScript to run with an onload event, and it now works on both my iPhone and iPad.

Next, we place that same webpage source code in a FileMaker global text field.  We create a web viewer with the object name “web” and specify a calculated URL:

"data:text/html," & 
geolocation::code

Using a URL that starts with data:text/html, tells FileMaker to treat the following text as an HTML page.  Essentially, it allows us to render a page from source code without needing a server and an HTTP request.

Our script resets the web viewer, waits a small amount of time to make sure the page has had time to render, and then checks the results with the formula GetLayoutObjectAttribute ( “web” ; “content” ).  If we have a result that contains <div id=“messageArea”><p>Latitude, bingo!

Now it’s just a simple matter to use FileMaker’s text functions to parse out the latitude and longitude.  We’ll need to do a little extra work to handle Apple’s data detectors—Mobile Safari sees the latitude and longitude numbers as possible phone numbers, and places <a href=“tel:...”> tags around them (UPDATE: We’ve updated the sample file to account for Mobile Safari not always detecting latitudes and longitudes as possible telephone numbers; our script sometimes gave us double negatives).  Once we have the latitude and longitude stored, an additional simple script can open a map to show us our location:

Open URL [No Dialog; "http://maps.google.com/maps?q=" & geolocation::latitude& ",+" & geolocation::longitude]

Log in or register to download the sample file.

Timothy Hall
08/04/2010

I downloaded the example file and put it up on my FM Server 11.
When I open it using FMGo on my iPad, and tell it to get location, it returns “Unknown” on all fields.

Chad Novotny
08/04/2010

Hmmm…  I’ve updated the HTML page so that the

  navigator.geolocation.getCurrentPosition(handler);

is now wrapped in another function, getLocation(), and getLocation is called in an onload event for the body element.  This seems to fix it on the iPad.  Thanks for catching this!

Jon Longworth
08/31/2010

Thank you Chad Novotny!  It works very well here (+45º -073º).

Is it possible to access the location services “speed” and “course” or “heading” using the same technique?

Chad Novotny
09/07/2010

Theoretically, you should be able to get those using something like:

messageArea. innerHTML+=”<p>Heading: ” + location.coords.heading + “</p>”;
messageArea. innerHTML+=”<p>Speed: ” + location.coords.speed + “</p>”;

(be sure to remove the spaces between “messageArea.” and “innerHTML”).  But I haven’t tested this.  Let us know if it works for you!

Jon Longworth
09/10/2010

No luck so far, I’m afraid.  Heading returns null and Speed is always 0 (although I wasn’t going fast enough to really test the speed).  Thanks for the html, however!

On a more important issue, the lat/lon values always default to the nearest cell tower and never improve.  It’s as if it’s taking one snapshot and not homing in on the currentl location (as it does in Maps or mobile Safari)…or it’s always resetting.  Is this to be expected?

Chad Novotny
09/11/2010

I think you need to be traveling at a measurable speed in order to get a heading reading as well.

To increate accuracy for the location values (at a possible speed expense), you can use an option called enableHighAccuracy.  Change the navigator.geolocation.getCurrentPosition line to:

navigator.geolocation.getCurrentPosition( handler, “”, {maximumAge: 90000, enableHighAccuracy: true} )

This will also tell the system to use reuse a position without recalculating so long as the previous position was determined in the last minute and a half (you can, of course, change the maximum age).

Lots more information can be found here:
http://diveintohtml5.org/geolocation.html

I also often reference this page:
http://www.thecssninja.com/javascript/geolocation-iphone

Jon Longworth
09/11/2010

Chad,  many thanks for the info and references smile

Jon Longworth
09/18/2010

Well, after a some reading and a lot of trial-and-error, I found that this works best and gives better accuracy:

data:text/html,<!DOCTYPE HTML>
<html>
  <head>

      <script type=“text/javascript”>

        function handler(location) {
          var message = document.getElementById(“messageArea”);
          messageArea. innerHTML=”<p>Latitude: ” + location.coords.latitude + “”;
          messageArea. innerHTML+=“Longitude: ” + location.coords.longitude + “”;
          messageArea. innerHTML+=“Accuracy: ” + location.coords.accuracy + “”;
        }
        function fail() {
      alert(‘Location unavailable’);
  }
        function getLocation() {
          navigator.geolocation.watchPosition(handler, fail, {maximumAge: 5000, enableHighAccuracy: true, timeout: 30000});
        }
       
      </script>

  </head>

  <body>
      <div id=“messageArea”>
        Location unknown
      </div>
  </body>

</html>


Note the change from navigator.geolocation.getCurrentPosition() to: navigator.geolocation.watchPosition()

Also, use Set Web Viewer (Object Name: “…” ; URL: htmlCode) instead of setting the web address of the web viewer to the htmlCode.  This avoids causing FM Go to quit unexpectedly.

Anders Thorssell
09/25/2010

Works very well when I access the file over a wifi network, but not when I access the file remotely from a FM Server 11 (it returns an “Unknown” in all fields)...

So what am I doing wrong?!

Gary Wallis
10/13/2010

Sorry… but I am at a loss as to how to get the Lat / Long content out of the Web Viewer and into a field.  If I get the info into a field I can then parse the individual Lat/Long.

Any help would be appreciated.

Jon Longworth
10/14/2010

@Gary,

Use the FileMaker scriptstep:
Set Variable [$variable; Value:GetLayoutObjectAttribute(“webViewerName”; “content”)

This puts the textual content of the web viewer into a variable which can then be parsed to put the latitude into one field and the longitude into another (or both into the same field if you prefer).

Log in or register to download the sample FileMaker document mentioned above.  It contains the scripts that do this.

Gary Wallis
10/14/2010

Jon,

Thanks for the ‘Tip’ on obtaining the web info….. and I did get the sample file which sort of works.  The captured location is off by several blocks.  That is until I access the real Safari Mobile… after doing that the program works flawlessly…. until I leave the application.  On returning I, once again, get the wrong location.  I tried using the highAccurancy code but using this only gets me the ‘Unknown’ for all fields.

I am using this on an iPhone 3.0.

Thanks for your help,

Gary

Bob Stuart
12/23/2010

I’ve been looking, but I can’t find the link to the download example…

Bob Stuart

Chad Novotny
12/23/2010

Hi Bob,

The link only shows up if your logged in, at the end of the article. Here it is again: http://www.supportgroup.com/?ACT=38&ID=209&FID=download

Kim Wyke
01/26/2011

I’m working to get the “enableHighAccuracy: true” option to work correctly and am having no luck.  This is definitely needed to get GPS level location and not just triangulated data.  Does anyone have an example of this working?  Without the option added to the getCurrentPosition call it works just fine.  As soon as I add it I get no results.  Any clues?  Thanks, Kim

Todd Scott
02/21/2011

I set up the web viewer as directed.  I do receive coordinates up running the script.  However, while I"m in FM Go, the coordinates remain constant even as I walk around hundreds of feet.  It’s not until I go to Safari Mobile and go to Maps does the system seem to re-calculate the coordinates.  Has anybody discovered a way to stay in FM Go and retrieve true coordinates?
Thanks,
Todd

Andrew Weinhold
02/28/2011

Thanks so much for this.  What are the units used for accuracy?  Meters?  Feet?

Jack Zhe
04/20/2011

Is this working with FM Go 1.2.1?  The WebViewer seems to show the latitude, longitude and accuracy, but when I use the GetLayoutAttribute(“MyWebViewer” ; “content” ) I’m getting this:

“<html><head></head><body></body><html>”

Is this a bug or has something changed in FM Go?  The exact same code worked yesterday on the same phone, same database, same everything.  All I did was update to FM Go 1.2.1.

Anyone else having issues?

Jack

Jon Longworth
04/21/2011

Jack,

FileMaker has acknowledged this bug with 1.2.1 and is working on it.  (Check the FileMaker Go forum for updates.)  It may take some time since new versions for the App Store have to be submitted for approval by Apple

Jack Zhe
04/22/2011

Thanks Jon…nice to have confirmation!  I sure am a bit disappointed with this release of FMG.  They fixed my iPad 2 camera problem, but killed my Coordinate collection capability and added a nice crash on Custom Dialog boxes for good measure.  I’m not usually hard on FM, but this release is very disappointing!

Jack

Jon Longworth
04/26/2011

Jack,

I suggest you voice your view on the FileMaker Go forum. The more people that add their weight behind this issue the better.  So far, only myself and one other have complained.

Silvano Rego
05/03/2011

I can’t get this working :-(
always “unknown” message :-(
is there any working solution for download?

Chad Novotny
05/03/2011

As Jon noted, this is currently broken in FileMaker 1.2.1 (view the FileMaker forum report at http://forums.filemaker.com/posts/1897d7d3b9). When it gets fixed, I’ll update the article.

There’s always a risk using “undocumented” features like this in FileMaker.  While this is definitely a bug, there’s always the chance that changes to behavior can break things.  For example, it’s quite possible that in a future version of Go, GetLayoutObjectAttribute will return the HTML source BEFORE the Javascript is processed, rather than after, as it does in FileMaker Pro (though I hope it ends up going the other way).

Chad Novotny
06/15/2011

FileMaker Go 1.2.2 has been released in the App Store (http://itunes.apple.com/us/app/filemaker-go/id379686220), which fixes the bug with GetLayoutObjectAttribute as well as a few others. Everything should be working again!

John Hermit
07/25/2011

Chad, thanks for this very useful tip and sample file.

BTW, you get mentioned in this official filemaker tech document : http://www.filemaker.com/products/filemaker-go/docs/techbrief_filemaker_go_121_en.pdf (on top of page 25)

Chad Novotny
07/25/2011

That’s awesome!  I had not seen that; thanks for bringing it to my attention!

Eric Matthews
09/06/2011

This works on the iPad 1 when I’m on Wi-Fi, but when I disconnect, it stops working. However NavFree and other applications continue to get my location even when I’m not connected to the internet. Why is this?

Calvente Rodolphe
01/10/2012

Hi! I’m a French user and having problem with “longitude”
In fact the longitude get the data without the “0” before the “,”
For exemple my location is :
Latitude = 49,5878 & Longitude = -0,59874
=> The data on filemaker retrieve -,59874 in Longitude and Google Maps don’t understand the data because of the “0” missing
Sorry for my bad english & i hope someone find how to put the missing “0”

Chad Novotny
01/10/2012

The following should insert any necessary zero and handle both positive and negative numbers:

Let ( long = GetAsNumber ( geolocation::longitude ) ;

  If (
    Int ( long ) = 0 ;
    Substitute ( “0” & long ; “0-” ; “-0” ) ;
    long
  )

)

Calvente Rodolphe
01/10/2012

Thank you for your help!
I have modify the code to add your answer and it work well

David Thomas
01/30/2012

I don’t understand why FileMaker go can’t access gps numbers from ios.  FM touch does it.  All you need to do is have a field named Gps.  Tap it in ions and it gets the latitude and longitude.  Simple.  They did it why cant FileMaker Go?

Chad Novotny
01/31/2012

David, I’m pretty sure FileMaker has received this request (but I don’t speak for FileMaker, so I could be off here). I think one of the issues they must deal with is feature parity between FileMaker Go and FileMaker Pro. They’ve worked really hard to make FileMaker Go do as much as FileMaker Pro can do, which does mean that iOS-specific features get added after others like importing or save as PDF.  As they round out the current Pro features in Go features, I imagine they will tackle more iOS features (such as their having added signature capture).

Aaron Ho
01/31/2012

Hello!

I’ve been trying to figure out the geolocation capabilities of FileMaker go and have tried to recreate this sample application.

For my test, I’ve only ported over the “get location” script and recreated all the fields and web viewer similar to the sample. However, when I run the sample, all the fields return with “unknowns”.

Now I am guessing that there is something off with my web viewer given that the “result” and “code” fields aren’t returning any values. In the sample, there is HTML tags in the fields but they are empty in my test.

What do you think is the issue?

Aaron

Jon Longworth
04/23/2012

FileMaker Pro/FileMaker Go version 12 now has a function for obtaining Latitude, Longitude and Altitude.

Chad Novotny
04/23/2012

Yes, all this is completely moot in FileMaker Go 12. As much fun as I had figuring this out, I’m glad to see geolocation now native in FileMaker Go.

Paolo Scaramuzzino
05/03/2012

Hi there, having problems with Go11 because it seems that global field is set by per-user session, therefore while being accessed by more than one, and the same, user the value of the global field used to contain the HTML5 code, will just disappear.
Any clue?

Chad Novotny
05/03/2012

Paolo, if you set the HTML5 content of the global field before hosting it, then it will be there every time the file is opened. If you need to be able to update it after hosting, you can create a utility table with a standard text field to store the HTML, and a startup script to set the global equal to the standard text field.

fabio surname
05/20/2012

I am trying to have this work on FM Go 11.0.1, I have downloaded your demo file too, but all I got is “unknow” for all fields :-(
Do I have to change something to have it work on this version of FM Go?

Thanks!

fabio surname
05/20/2012

...I have found an error in the HTML code, I mean in the javascript code: the variable “message” become “messageArea” so it will always fail.
The right script should be:

        function handler(location) {
          var message = document.getElementById(“messageArea”);
          message[removed]=“Latitude: ” + location.coords.latitude + “”;
          message[removed]+=“Longitude: ” + location.coords.longitude + “”;
          message[removed]+=“Accuracy: ” + location.coords.accuracy + “”;
        }
       
        function fail() {
          var message = document.getElementById(“messageArea”);
          message[removed]=“Can’t get located”
        }
       
        function getLocation() {
          navigator.geolocation.watchPosition(handler,fail,{maximumAge: 5000, enableHighAccuracy: true, timeout: 30000});
        }
unfortunately this is not enought for me becase I am still unable to get location from FM Go. It does work in browsers.

fabio surname
05/21/2012

here again, after some testing it seems that in FM Go 11.0.1 this piece of script does not work:

navigator.geolocation.watchPosition()

geolocation seems to be supported but the watchPosition is not returning any valid resul.

Help!?

Chad Novotny
05/21/2012

Fabio, thank you for your comments. Update 3 above should contain code for the web viewer that works. As for watchPosition, it should work; it does on my devices.

Please re-download the sample file and see if that works. You may also need to enable location services for FileMaker Go in your iOS Settings.

fabio surname
05/22/2012

Thanks you!

I was missing to allow location in iOS settings!

Mark Stuller
07/19/2012

Thanks for the great work Chad. Very useful.

I have a challenge that while I don’t think this releated to the solution I’d like to offer it up for others review.

We’ve deployed a solution w/ Go 11.0.1 and iOS 5.1.1 on iPhone 4s’s. Occasionally this will stop working with a message to the user ‘No GPS Signal’. Of course then no Lat/Lon is available to scrape off the webviewer. Other iOS apps (Maps for instance) still get GPS but FileMaker Go no longer does. The sure fix is to remove and reinstall Go (and the db) but as you can imagine that’s not a very palatable fix.

I believe this is an iOS/Go issue not a technique issue but I tho’t I’d throw it out there for others that may encounter this and for comments if anyone has them.

Thanks again Chad. Great solution.

Omar Almeida
02/15/2013

Thanks, great example Chad!


Congrats

Adrian Roman
02/14/2014

It doesn’t work with FileMaker Instant Web Publishing.

Would be possible do compatible?

Laurent Ades
05/31/2014

Hi,

I know FileMaker Go now handles that all very well…

Nonetheless, what about designing solutions that can be accessed either by mobile users on FMGo or non-iOS users (Windows, Linux, Android…Etc…) for which web direct (FMS13) is the best option ?

I’m trying to apply this strategy and so far, the HTML/JS script works well when running on a desktop machine as a standalone html page, but when integrating it in a FMPro 13 web viewer, and ruing it, the web viewer simply displays “Cant get located”, which somewhat tells me it cannot access the location service. Is that meant not to work onFMPro 13 whilst the same html/js code works a snap in Safari ?

Any idea ?

Log in as a member to comment...