Rounding Lat and Long to Show Approximate Location in Google Maps(舍入经纬度以在 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.
这篇关于舍入经纬度以在 Google 地图中显示大致位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:舍入经纬度以在 Google 地图中显示大致位置
基础教程推荐
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
