Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounding Lat and Long to Show Approximate Location in Google Maps

I'm displaying a Google map in my Rails app's view and will be using a marker/overlay.

The coordinate data will be coming from a phone (GPS) and stored in my Rails app's db.

The problem is, I don't want the precise lat/long visible in the source of my web page ... i.e. I want to mark an approximate location without giving away the true lat/long.

How can I round/truncate the lat/long values so they are still accurate (say, within a mile)–but not too accurate?

(Example: how would you round 35.2827524, -120.6596156 ?)

like image 753
Callmeed Avatar asked Mar 11 '11 06:03

Callmeed


1 Answers

The easiest thing to do would be either to round both coordinates to a certain number of decimal places, or add a random dither to the coordinates:

lat = Math.floor(lat*1000+0.5)/1000; (round within 0.001)

or

dither=0.001;
lat = lat + (Math.random()-0.5)*dither;

If you really want to be within a certain number of miles, you'd need to do more-complex math, probably using polar coordinates.

like image 122
Mark Bessey Avatar answered Oct 13 '22 01:10

Mark Bessey