distance

Description

Calculate the distance between two latitude/longitude points.

Copy
float distance(float latitude_1, float longitude_1, float latitude_2, float longitude_2[, string units])

latitude_1 and longitude_1 represent a coordinate pair

  • Coordinates must be floats, and can be inserted as saved variables. Passing a string results in an error.
  • Units is optional, and can be either "mi" (miles) or "km" (kilometers). The default value is miles. Quotation marks are required.

Using this function, you can tailor content based on user location.

Examples

Distance in Miles

{distance(40.7267926,-74.0053737,40.500819,-74.447399)}

Distance in Kilometers

{distance(40.7267926,-74.0053737,40.500819,-74.447399,"km")}

Distance Between Location and Profile Var Coordinates

{distance(40.7267926,-74.0053737,profile.vars.latitude,profile.vars.longitude)}

Note: If you have latitude and longitude saved as strings on the user’s profile, you will first need to convert them to numbers.

Conditional Message Using Distance

In the following example, the message “Point B is within 10 miles of Point A!” only appears if the Point B is less than 10 miles from Point A.

{if {distance(40.7267926,-74.0053737,40.500819,-74.447399) < 10}
Point B is within 10 miles of Point A! 
{/if}

Display Content from a Feed Based on a User’s Geolocation

Use Case:

You’re using the sailthru.location meta tag to place latitude and longitude on your content, and you want to only display content if the user is geo-located within 20 miles of that item.

Zephyr:

{**Creates an array of user lat/long, coords[0] being lat, coords[1] being long**}
{coords = user_geo_home("coordinates")}
Stories in Your Neighborhood:
{foreach content as c}
  {if contains(c,c.location)}
      {if distance(c.location[0], c.location[1], coords[0], coords[1]) > 20}
<a href="{c.url}">{c.title}<a/> ({round(distance(c.location[0],c.location[1],coords[0],coords[1]))} from you!) <br/>
{/if}
  {/if}
{/foreach}

Output:

Stories in Your Neighborhood:


(New York City based user):

Get Your First Look at the New iPhone at the Times Square Apple Store (2.1 miles you from!)

Spider-Man: Threat or Menace? (7.2 miles from you!)

(Los Angeles based user):

Alexander Harris elected to Los Angeles House of Representatives (12.8 miles from you!)

Explanation:

This script uses the user_geo_home() function to find the coordinates of a user’s Sailthru-tracked geolocation (obtained at point of click through an email). It then assigns this value to a local variable called “coords,” which is an array of the user latitude (position 0) and longitutde (position 1). Next, a foreach loop iterates through each piece of content from a data feed and, using an “if statement”, uses the contains() function to see if each content has the “location” parameter. If it does, using a second “if” statement, the script uses the distance() function to check if if the content’s latitude/longitude (c.location[0] and c.location[1] is within the user’s latitude/longitude (coords[0] and coords[1].

If the statement evaluates to true, the title and URL will populate and display to the user. The script also uses the round() function to tell the user how close they are from the story. The distance from the user is calculated the same as before (using the item’s coordinates evaluated against the user’s), but the actual distance is rounded to the nearest decimal point in order to display a user-friendly version of how close the user is.