[Visual Basic] 纯文本查看 复制代码
Public Function encodeGeoHash(Latitude, Longitude, Precision)
Dim BITS() As Variant
Dim BASE32() As Variant
BITS = Array(16, 8, 4, 2, 1)
BASE32 = Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", _
"b", "c", "d", "e", "f", "g", "h", "j", "k", "m", _
"n", "p", "q", "r", "s", "t", "u", "v", "w", "x", _
"y", "z")
Dim isEven As Boolean
isEven = True
i = 0
Bit = 0
ch = 0
Geohash = ""
Dim latMin As Double, latMax As Double
Dim lonMin As Double, lonMax As Double
latMin = -90#
latMax = 90#
lonMin = -180#
lonMax = 180#
Dim tempMid As Double
While (Len(Geohash) < Precision)
If isEven Then
tempMid = (lonMin + lonMax) / 2
If Longitude > tempMid Then
ch = ch Or BITS(i)
lonMin = tempMid
Else
lonMax = tempMid
End If
Else
tempMid = (latMin + latMax) / 2
If Latitude > tempMid Then
ch = ch Or BITS(i)
latMin = tempMid
Else
latMax = tempMid
End If
End If
isEven = Not isEven
If i < 4 Then
i = i + 1
Else
Geohash = Geohash & BASE32(ch)
i = 0
ch = 0
End If
Wend
encodeGeoHash = Geohash
End Function