問題描述
我正在我的 Rails 應(yīng)用程序視圖中顯示 Google 地圖,并將使用標(biāo)記/覆蓋.
I'm displaying a Google map in my Rails app's view and will be using a marker/overlay.
坐標(biāo)數(shù)據(jù)將來自手機(jī) (GPS) 并存儲(chǔ)在我的 Rails 應(yīng)用程序的數(shù)據(jù)庫中.
The coordinate data will be coming from a phone (GPS) and stored in my Rails app's db.
問題是,我不希望在我的網(wǎng)頁源中顯示精確的緯度/經(jīng)度……也就是說,我想標(biāo)記一個(gè)大概的位置,而不泄露真正的緯度/經(jīng)度.
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.
如何舍入/截?cái)嗑暥?經(jīng)度值,使它們?nèi)匀粶?zhǔn)確(例如,在一英里內(nèi)),但又不太準(zhǔn)確?
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 ?)
推薦答案
最簡單的做法是將兩個(gè)坐標(biāo)四舍五入到一定的小數(shù)位,或者在坐標(biāo)上添加隨機(jī)抖動(dòng):
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;
如果你真的想在一定的英里數(shù)內(nèi),你需要做更復(fù)雜的數(shù)學(xué),可能使用極坐標(biāo).
If you really want to be within a certain number of miles, you'd need to do more-complex math, probably using polar coordinates.
這篇關(guān)于舍入經(jīng)緯度以在 Google 地圖中顯示大致位置的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!