File Exchange

## Google Maps API Polyline Decoder

version 1.1.0.0 (2.98 KB) by chris hinkle

### chris hinkle (view profile)

Decodes google maps API polyline string into lat/lon positions.

Updated 03 Aug 2011

Takes a single string array which follows the google maps API encoded polyline string and parses it into arrays of lat/lon pairs. Algorithm documentation from google can be found at http://code.google.com/apis/maps/documentation/utilities/polylinealgorithm.html

### Cite As

chris hinkle (2020). Google Maps API Polyline Decoder (https://www.mathworks.com/matlabcentral/fileexchange/32341-google-maps-api-polyline-decoder), MATLAB Central File Exchange. Retrieved .

Mike Scannell

### Mike Scannell (view profile)

Agree with Alex B's comment: Replacing the num2str and bin2dec calls with bi2de significantly speeds this up.

The downside of using de2bi and bi2de is that it requires the communication toolbox, which not everyone may have.

The function only seems to be outputting latitudes... not longitudes.

Alex B

### Alex B (view profile)

Hi Chris,

THanks for uploading this code it helped me a lot. It has a slow part however, it uses strings for binary to decimal, it can be sped up using the function bi2de(asciiBin1,'left-msb') instead of these slowies: num2str, bin2dec

% % for i = 1:length(asciiBin1)
% % if i == 1
% % stringVal = num2str(asciiBin1(i));
% % else
% % stringVal = [stringVal,num2str(asciiBin1(i))];
% % end
% % end
% % decVal = bin2dec(stringVal);

can be replaced by (2015):
decVal = bi2de(asciiBin1,'left-msb');

Neil Abroug

### Neil Abroug (view profile)

%-------------------------------------------------------------------------%
function [decVal] = getSingleLine(asciiIn)
%break ascii down to individual numeric points
asciiNum = zeros(size(asciiIn));
for i = 1:length(asciiIn)
asciiNum(i) = double(asciiIn(i));
end
%subtract 63 from each point
asciiNum = asciiNum-63;

%convert to binary
asciiBin = dec2bin(asciiNum);
% un-or the 0x20 from each value
if size(asciiBin,1) > 1
asciiBin = asciiBin(:,2:size(asciiBin,2));
%reverse the order of the 5 bit chunks
asciiBin1 = asciiBin(end:-1:1,:);
else
asciiBin1 = asciiBin;
end
asciiBin1 = asciiBin1';
asciiBin1 = asciiBin1(:);
% check if original decimal value is negative
if asciiBin1(end) == '1' %negative, need to invert the coding ***********check this value
asciiBin1 = num2str(1-str2num(asciiBin1));
isNeg = 1;
else %positive
isNeg =0;
end
% catch
% a = 5;
% end
%right shift binary value one bit, zero pad
asciiBin1 = asciiBin1(:);
% lengthVal = 8-mod(length(asciiBin1),8)+1;
if length(asciiBin1) > 1
asciiBin1 = asciiBin1(1:end-1);
else
asciiBin1 = '0';
end
%if first value is 1, negative, need to take 2's complement
if isNeg%asciiBin1(end) == 0
asciiBin1 = num2str(1-str2num(asciiBin1));
if length(asciiBin1)==1
plusOne = 0;
else
plusOne = 1;
end
else
plusOne = 0;
end

stringVal=asciiBin1';
decVal = bin2dec(stringVal);
decVal = decVal+plusOne;
if isNeg
decVal = -1.*decVal;
end
decVal = decVal / 1e5;

return

Neil Abroug

### Neil Abroug (view profile)

without these modification, I get error when passing a google polyline to the function

chris hinkle

### chris hinkle (view profile)

Neil,

I would not consider the function as a polyline decoder having an error, as it properly decodes a google polyline. I do, however, need to properly describe the output detailed in the Google polylinealgorithm api in the .m file.

I may add the option for cumsum if people want lat lon coordinates, or if people want the raw output.

Thanks for the feedback.

Neil Abroug

### Neil Abroug (view profile)

There are some errors in getSingleLine.
cumsum should be applied to latOut and lonOut because only the twoo first coordinates are absolutes. the nexst ones ares relative coordinate according to the previous one.