舍入纬度和长度以显示Google地图中的大致位置 [英] Rounding Lat and Long to Show Approximate Location in Google Maps

查看:83
本文介绍了舍入纬度和长度以显示Google地图中的大致位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Rails应用视图中显示Google地图,并使用标记/叠加层。

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



问题是,我不希望在我的网页源代码中看到精确的lat / long ...即,我想标记大致的位置而不给出远离真正的经纬度。

我怎样才能四舍五入/截断lat / long值,使它们仍然准确(比如,在一英里之内) - 但不是太准确?



(例如:你会如何轮到35.2827524,-120.6596156?)

解决方案

最简单的做法是将两个坐标都舍入到一定数量的小数位,或者向坐标中添加一个随机的仿色:

  lat = Math.floor(lat * 1000 + 0.5)/ 1000; (在0.001内舍入)



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

如果您真的想要达到一定的英里数,您需要做更多 - 复杂的数学,可能使用极坐标。

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 ?)

解决方案

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.

这篇关于舍入纬度和长度以显示Google地图中的大致位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆