# 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 11, 2014 at 12:21 PM
Compound Transclusion Prevented In AngularJS 1.2
Yeah this hit our team too and locked us as 1.1.5. It seemed like an irresponsible unilateral decision, but our graver concern is the future decisions the Angular team will make with other fundamenta ... read »
Mar 11, 2014 at 2:00 AM
ColdFusion, jQuery, And "AJAX" File Upload Demo
Mar 10, 2014 at 8:24 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Steven, the action property is not something from AngularJS but rather part of the concept that Ben is describing in his blogpost here. it is a custom added property, which is read by the requestCo ... read »
Mar 10, 2014 at 2:03 PM
Nested Views, Routing, And Deep Linking With AngularJS
Where is the angular documentation for 'action:' it is not here: http://docs.angularjs.org/api/ngRoute/provider/ \$routeProvider Thanks. ... read »
Mar 10, 2014 at 12:06 PM
Using Track-By With ngRepeat In AngularJS 1.2
I was hoping that this will work with pagination using ng-repeat. My use case scenario is that I have an images object. images[0] = [im1,im2,im3] // First Page images[1] = [im4, im5, im6] // Second ... read »
Mar 9, 2014 at 6:11 PM
For Better Security Use HtmlEditFormat() In Conjunction With JSStringFormat() In ColdFusion
It looks like htmleditformat() will be deprecated in CF 11 https://wikidocs.adobe.com/wiki/display/coldfusionen/New +in+ColdFusion ... read »
Mar 9, 2014 at 10:55 AM
\$.stop() vs. \$.finish() In jQuery Animations
Nice feature! Thanks for sharing. :) Good for when you are making a 100% AJAX controlled site. ... read »
Mar 9, 2014 at 12:26 AM
Experimenting With Multiple Class Inheritance In Javascript
@bigboomshakala, Hi, I am want present so solution for multiple prototypical inheritance, but some restrictions. Please follow the link: http://arto8.site11.com/ArtoJS/multi-inheritance-prototype.ph ... read »