Skip to content

Instantly share code, notes, and snippets.

@rajtrivedi2001
Forked from stormxuwz/Google API.m
Created December 29, 2015 12:32
Show Gist options
  • Save rajtrivedi2001/29a9060cb69773cd5414 to your computer and use it in GitHub Desktop.
Save rajtrivedi2001/29a9060cb69773cd5414 to your computer and use it in GitHub Desktop.
Matlab Google Street View and Direction API
origin='Madison,Chicago,IL';
destination='N+Michigan+Ave,Chicago,IL';
url1=['https://maps.googleapis.com/maps/api/directions/json?origin=' origin '&destination=' destination '&sensor=false&key= XXXX&departure_time=1343641500&mode=driving'];
direction=urlread(url1);
d=parse_json(direction);
steps=d{1}.routes{1}.legs{1}.steps;
[startpoints,endpoints]=cellfun(@parsecoord,steps);
for i=1:numel(steps) % For each step, find the midpoint and save the street view image
sp=startpoints(i);
spLat=sp.lat;
spLong=sp.lng;
ep=endpoints(i);
epLat=ep.lat;
epLong=ep.lng;
mpLat=(spLat+epLat)/2;
mpLong=(spLong+epLong)/2;
%directiondegree=azimuth(spLat,spLong,epLat,epLong); % calculate the azimuth,i.e. the direction of car
directiondegree=0;
roadLat=mpLat;
roadLong=mpLong;
heading=directiondegree;
url=['http://maps.googleapis.com/maps/api/streetview?size=640x640&location=' num2str(roadLat) ',' num2str(roadLong) '&sensor=True&key=XXXX&heading=' num2str(heading)];
filename=[num2str(i) '_test.jpg'];
urlwrite(url,filename);
%b=imread(filename);
%image(b)
end
@rajtrivedi2001
Copy link
Author

Create a new file with following code:
and save it with name 'parsecoord.m' in bin directory
function [startpoints,endpoints] = parsecoord(input)
%parsecoord parse the cooordinate
startpoints = input.start_location;
endpoints =input.start_location;
end

@rajtrivedi2001
Copy link
Author

I am receiving following error:
Error using urlreadwrite (line 98)
Error downloading URL. Your network connection may be down or your proxy settings improperly configured.

Error in urlwrite (line 38)
[f,status] = urlreadwrite(mfilename,catchErrors,url,filename,varargin{:});

Error in Untitled (line 37)
urlwrite(url,filename);

@rajtrivedi2001
Copy link
Author

Solved by changing API key

@tangshuo60
Copy link

sorry, can you upload the whole code? the error is that I do not define 'd=parse_json(direction)'. thank you

@tangshuo60
Copy link

what's the parse_jason function?

@Tala22
Copy link

Tala22 commented Aug 20, 2017

Could you please guide me to change this code to use Google Translation API , to translate text using that API during MATLAB?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment