Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#https://developers.google.com/maps/documentation/utilities/polylinealgorithm?hl=ja
def decodeG(asciiG):
print("decode start")
combine=0
i=0
for c in asciiG:
eachdecimal = ord(c)-63
mask = 31#b11111
extracted = eachdecimal & mask
combine = combine + (extracted<<i)
i = i + 5
print(eachdecimal)
print("combine="+str(combine)+" ~combine="+str(~combine))
#right shift
rshift = combine>>1
print("right shift = "+str(rshift)+" ~rshift="+str(~rshift))
print("end")
def encode(latlonlist):
prevLat =0.0
prevLon=0.0
result=""
num = len(latlonlist)
for i in range(0,num,2):
lat = float(latlonlist[i])
lon = float(latlonlist[i+1])
latstr = encodeG(lat-prevLat)
lonstr = encodeG(lon-prevLon)
prevLat = lat
prevLon = lon
result = result + latstr+lonstr
return result
def encodeG(latlondiff):
converted = int(latlondiff * 1e5)
if(converted==0):
converted = 1
#Left-shift the binary value one bit:
leftshifted = converted <<1
#If the original decimal value is negative, invert this encoding:
if(converted<0):
leftshifted = ~leftshifted
encoded =""
mask = 31#b11111
while(leftshifted>0):
#Break the binary value out into 5-bit chunks (starting from the right hand side):
extracted = leftshifted & mask
if(leftshifted>31):
o = extracted | 0x20
else:
o = extracted
a = o +63
encoded = encoded + chr(a)
leftshifted = leftshifted>>5
return encoded
input ="35.69749000 139.57235100 35.69749400 139.57235200"
splitted = input.split(' ')
st = encode(splitted)
start ="https://maps.googleapis.com/maps/api/staticmap?size=400x400&path=weight:3|color:blue|enc:"
print(start+st)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment