舍入经纬度以在 Google 地图中显示大致位置

我正在我的 Rails 应用程序视图中显示 Google 地图,并将使用标记/覆盖.

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

坐标数据将来自手机 (GPS) 并存储在我的 Rails 应用程序的数据库中.

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?

(例如:你将如何舍入 35.2827524, -120.6596156 ?)

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

推荐答案

最简单的做法是将两个坐标四舍五入到一定的小数位,或者在坐标上添加随机抖动:

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)

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.

相关文章