# Creating Fuzzy Latitude And Longitude Values With A Given Amount Of Freedom

Posted February 7, 2011 at 10:01 AM by Ben Nadel

Recently, I added location tracking to the Things-I-Give web application. As I talked about before, ThingsIGive.com allows users to track their own good deeds in an effort to "gamify" the act of giving; the hope, of course, being that users will become greedy for giving (built in an effort to combat my own anxiety about giving). Although the web application is completely anonymous, I know that people are still a bit weary of giving out location information. As such, I wanted to take the global position reported by the web application and make it a bit "fuzzy."

When the browser reports its location, either through the Javascript Geolocation API or its remote IP address, the location is defined as latitude and longitude coordinates. These are given in degrees. I wanted to create a function that would take a latitude-longitude point and a given "miles of freedom" and then return a random position based on the miles of freedom.

Doing this simply required a little math to figure out how many miles-per-degree were available at the given latitude and longitude. Here's what I came up with:

• <cffunction
• name="fuzzyLatLong"
• access="public"
• returntype="struct"
• output="false"
• hint="I make the given latitude and longitude values a tiny bit fuzzy depending on the given miles of freedom.">
•
• <!--- Define arguments. --->
• <cfargument
• name="latitude"
• type="numeric"
• required="true"
• hint="I am the Float value indicating one of the latitdue degree value."
• />
•
• <cfargument
• name="longitude"
• type="numeric"
• required="true"
• hint="I am the Float value indicating one of the longitude degree value."
• />
•
• <cfargument
• name="miles"
• type="numeric"
• required="false"
• default="5"
• hint="I am the number of miles of 'fuzziness' that we we'll use in our latitude / longitude randomization."
• />
•
• <!--- Define the local scope. --->
• <cfset var local = {} />
•
• <!---
• Get the number of miles per degree. This is only an estimate
• based on latitude. The longitude will have to be determined
• based on teh latitude.
• --->
• <cfset local.milesPerDegreeLatitude = 69.09 />
•
• <!---
• Now that we have the miles per degree of latitude, we need to
• figure out what the same distance is for the longitude at the
• given latitude.
•
• NOTE: This forumla was taken from the following Google
• --->
• <cfset local.milesPerDegreeLongitude = (local.milesPerDegreeLatitude * cos( arguments.latitude * pi() / 180 )) />
•
• <!---
• Determine the number of degrees that we will need to
• randomize the latidude based on the miles of freedom.
• --->
• <cfset local.degreesOfLatitudeRandomness = (arguments.miles / local.milesPerDegreeLatitude) />
•
• <!---
• Determine the number of degrees that we will need to
• randomize the longitude based on the miles of freedom.
• --->
• <cfset local.degreesOfLongitudeRandomness = (arguments.miles / local.milesPerDegreeLongitude) />
•
• <!---
• Create a struct to hold the fuzzy version of the
• latitude and longitude values. For each of these values,
• we'll subtract the maximum randominess then add a fraction
• of 2x the randomness. This will give us a random value
• between our negative random max and our positive random max.
• --->
• <cfset local.coordinates = {
• latitude = (
• (arguments.latitude - local.degreesOfLatitudeRandomness) +
• (
• local.degreesOfLatitudeRandomness *
• (2 * rand( "sha1prng" ))
• )
• ),
• longitude = (
• (arguments.longitude - local.degreesOfLongitudeRandomness) +
• (
• local.degreesOfLongitudeRandomness *
• (2 * rand( "sha1prng" ))
• )
• )
• } />
•
• <!--- Return the resultant coordinate value. --->
• <cfreturn local.coordinates />
• </cffunction>
•
•
• <!--- ----------------------------------------------------- --->
• <!--- ----------------------------------------------------- --->
•
•
• <cfoutput>
•
• <!DOCTYPE html>
• <html>
• <title>Creating Fuzzy Latitude And Longitude Values</title>
• <body>
•
• <!--- Our map container. --->
• <div id="map" style="height: 950px ;"></div>
•
• <script type="text/javascript">
•
• // Set up the Google map stuff.
• var mapContainer = document.getElementById( "map" );
•
• var map = new google.maps.Map(
• mapContainer,
• {
• zoom: 13,
• 40.725665,
• -73.996353
• ),
• }
• );
•
•
• // I add maker to the map and initialize it such that it
• // will respond to click events.
• var addMarkerToMap = function( latLong ){
• // Create new marker from the location.
• var marker = new google.maps.Marker({
• map: map,
• position: latLong
• });
•
• // Return the newly created marker.
• return( marker );
• };
•
•
• // ---------------------------------------------- //
• // ---------------------------------------------- //
•
•
• <!--- Add vertical markers. --->
• <cfloop index="i" from="1" to="10">
•
• <!---
• Get our fuzzy position with a fuzzy freedomness
• of one mile.
• --->
• <cfset position = fuzzyLatLong(
• 40.725665,
• -73.996353,
• 1
• ) />
•
• <!--- Keep the longitude constant. --->
• #position.latitude#,
• -73.996353
• )
• );
•
• </cfloop>
•
•
• <!--- Add horizontal markers. --->
• <cfloop index="i" from="1" to="10">
•
• <!---
• Get our fuzzy position with a fuzzy freedomness
• of one mile.
• --->
• <cfset position = fuzzyLatLong(
• 40.725665,
• -73.996353,
• 1
• ) />
•
• <!--- Keep the latitude constant. --->
• 40.725665,
• #position.longitude#
• )
• );
•
• </cfloop>
•
• </script>
• </body>
• </html>
•
• </cfoutput>

As you can see, this function, fuzzyLatLong(), essentially determines how many degrees of freedom there are at the given location based on the given number of "fuzzy" miles. Once the degrees of freedom have been found, it then becomes a matter of randomly selecting a point between the -MAX and MAX degree values.

When we run the above page, we get the following Google Maps output:

As you can see, these points have been randomly distributed within a mile of the original position (where the two lines intersect). In this case, I have only randomized one of the coordinates in order to illustrate the distribution. However, when put into practice, both the latitude and longitude would be randomized.

### Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

Feb 7, 2011 at 6:09 PM // reply »

We do something similar to mask real estate values for agents who wish to not display the exact address... Our geocodes would ruin that, so we randomize it within .05 miles, which ends up being 1-2 blocks.

We also make it a polygon circle to encompass that address. Pff. Real Estate :P

Feb 7, 2011 at 6:13 PM // reply »

Makes sense - you don't want to make it too easy for people to circumvent the agent. Doing a circle would probably be better; I assume the math gets a bit more complicated for that :)

Feb 7, 2011 at 6:21 PM // reply »

Well, we did it super simple. We offset the center much like you did, then drew a .075-.1 size circle with the center based on that point.

Because, even a circle alone you'd be able to find the center and circumvent the agent so the offset really helps.

It might be more paranoia then necessity, but that was the build request. REBNY (real estate board) actually requires that you limit returned search results and stuff... RE projects are the hardest.

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 6, 2014 at 8:58 PM
Posting XML With ColdFusion, CFHttp, And CFHttpParam
ERROR2: Missing type node :( ... read »
Mar 6, 2014 at 6:56 AM
CFLoop Attributes Evaluated Only Once
Hi Ben, An "oldie but a goodie"... funny enough we just had a discussion about this at work, what with JavaScript evaluating at each iteration of a for loop it stems to reason that CF woul ... read »
Mar 5, 2014 at 6:27 AM
Defining Instantiatable Classes In The AngularJS Dependency Injection Framework
Mar 5, 2014 at 2:29 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Jamie, Thanks a lot. It is really helpful. :). ... read »
Mar 4, 2014 at 2:07 PM
Converting Dates Across Time Zones Using ColdFusion And Java
Ben: I am using this and it is working great locally but when i post it to my production server it is not working. I am using osx locally and centos in production. any idea why this is not working ... read »
Mar 4, 2014 at 11:19 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Himaja, This talk from Google's DoubleClick team, at ng-conf, may provide some ideas for you: "Writing a Massive Angular App at Google" - http://youtu.be/62RvRQuMVyg There's a good chanc ... read »
Mar 4, 2014 at 9:34 AM
The Magic Of Thinking Big By David Schwartz (Thanks Clark Valberg!)
@Neha, I read the book fifty years ago. Still fresh in my mind. Just a couple of weeks ago a friend I gave a copy to, not long ago, told me she sent a copy to a person close to her who recently lost ... read »
Mar 4, 2014 at 2:38 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@muhannad, It is a web application. It will have user hierarchy. Users can create content. They can create playlists with images, videos, html content. They can set the start date and end date. Thos ... read »