This is Part 4 in a multi-part series about using the location functions in FileMaker Go.
So far in this series, I’ve described how to get geographic coordinates for your current location using FileMaker Go running on an iPhone or iPad, and I’ve described how to get coordinates for the addresses that matter to you using geocoding. The mathematician in me exclaims, “Hooray! Numbers!” The engineer in me responds with a less enthusiastic, “Numbers — so what?” It’s great that the Location and LocationValues functions can tell me my latitude and longitude, but those coordinates only tell me my absolute position on the planet. Who cares? What’s usually useful is my location relative to the stuff I care about. Coordinates are just data; what I really want to know is whether or not I’m getting any closer to the party.
Given the coordinates of my current location and the coordinates of another location, the first thing I want to know is how far away I am. Assuming that you don’t need to aim a dart at a target on the other side of the planet, the haversine formula is good enough for calculating straight line (“great circle”) distances between locations. Here’s my version of the formula written to use FileMaker functions and to simplify some of the math.
6378137 // equatorial radius of Earth in meters according to WGS 84 * Acos ( Cos ( Radians ( $latitude2 - $latitude1 ) ) - Cos ( Radians ( $latitude1 ) ) * Cos ( Radians ( $latitude2 ) ) * ( 1 - Cos ( Radians ( $longitude2 - $longitude1 ) ) ) )
Transit Distance and Time
The haversine formula works fine for calculating straight-path distances between two points — distance “as the crow flies.” However, we’re often more practically interested in time “as the car drives.” As with geocoding, we can use a Google web service API for this, the Google Distance Matrix API. As before, we calculate a URL of our request to the API. The API accepts either coordinates or addresses for origins and destinations.The API also allows you to specify whether you want distance and time for driving, walking, or bicycling. To simplify this example, I’m not specifying a transit method, and I’m only using coordinates for the origin and an address for the destination.
"https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & $originLatitude & "," & $originLongitude & "&destinations=" & GetAsURLEncoded ( $destinationAddress ) & "&sensor=" & If ( Get ( SystemPlatform ) = 3 ; // only iOS devices have location sensors "true" ; /* Else */ "false" )
After getting the API response using the Insert from URL script step, we convert the API’s response from JSON to Let notation using the JSON module by Dan Smith, and we extract the time and distance values from the result.
If you want to use any of these techniques in your own applications, you’re welcome to copy from the demo file for this post: Location.fmp12